Search code examples
.netxpathweb-configweb-config-transformslowcheetah

Select node based on child node value in Web.config Transform


I have the following XML in my web config and I would like to select an attribute for removal using web.config transforms, but I would like to select the element for removal based on the value of one of the child elements.

My web.config is something like this:

<configuration>
   <sitecore>
       <scheduling>
          <agent type="Sitecore.Tasks.DatabaseAgent">
             <param desc="database">core</param>
          </agent>
          <agent type="Sitecore.Tasks.DatabaseAgent">
             <param desc="database">master</param>
          </agent>
       </scheduling>
    </sitecore>
 </configuration>

I have tried the following to try to select the second agent element for deletion based on the child element <param desc="database">master</param> but with no success.

<configuration>
   <sitecore>
       <scheduling>
          <!-- Attempt 1 -->
          <agent type="Sitecore.Tasks.DatabaseAgent"
                 xdt:Transform="Remove"
                 xdt:Locator="XPath(configuration/sitecore/scheduling/agent/param[text()='master'])"/>

          <!-- Attempt 2 -->
          <agent type="Sitecore.Tasks.DatabaseAgent"
                 xdt:Transform="Remove">
             <param desc="database"
                    xdt:Locator="XPath([text()='master'])"/>
          </agent>
       </scheduling>
    </sitecore>
 </configuration>

Solution

  • As answered in this question the xdt:Locator attribute needs to use the Condition syntax. So the required selector is:

    <agent type="Sitecore.Tasks.DatabaseAgent"
           xdt:Transform="Remove"
           xdt:Locator="Condition(param/@desc='database' and param/text()='master')" />