Select node based on child node value in Web.confi

2019-03-20 21:01发布

问题:

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>

回答1:

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')" />


回答2:

Just use Sitecores own config patcher. This will remove your setting:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <scheduling>
       <agent patch:instead="*[@type='Sitecore.Tasks.DatabaseAgent' and param='master']">
       </agent>
    </scheduling>
 </sitecore>
</configuration>

For more information, look here:

http://intothecore.cassidy.dk/2009/05/working-with-webconfig-include-files-in.html http://www.thescrewballdivision.com/playing-with-sitecore-include-files



回答3:

Just add /.. onto the end, that should do it..

e.g.

XPath(configuration/sitecore/scheduling/agent/param[text()='master']/..)