Search code examples
xpathweb-config-transform

Web config transformation condition/match to select a node based on parent node attribute


I have a transform that looks like this

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <a>
    <b>
      <c>
        <d>
          <e name="UpdateLanguageProfile">
            <f xdt:Transform="Replace" xdt:Locator="Condition(/..@name='UpdateLanguageProfile')">
              stuff here
            </f>
          </e>
        </d>
      </c>
    </b>
  </a>

So I want the xdt:Locator to select the f node only if the parent node has an attribute with the specified value.

The xdt:Locator gets translated into the following xpath expression:

/a/b/c/d/e/f[/..@name='UpdateLanguageProfile']

Which is invalid.

So the question is, what could I put in the Condition, that is the XPath square brackets, in order to select the f node based on an attribute in the parent node.


Solution

  • The answer is that the xdt:Locator and the xdt:Transform do not need to be on the same node. They just happen to be on the same node in every example I've ever seen.

    You can do this:

    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <a>
        <b>
          <c>
            <d>
              <e name="UpdateLanguageProfile" xdt:Locator="Match(name)">
                <f xdt:Transform="Replace">
                  stuff here
                </f>
              </e>
            </d>
          </c>
        </b>
      </a>