问题
In other words, in the following web.config xml, I want to remove all elements with a type attribute that starts with 'Elmah.'
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>
I have tried several combinations and structures of the basic transform, with various errors,
<add xdt:Locator="XPath([starts-with(@type,'Elmah.')" xdt:Transform="Remove"/>
before giving up and just removing the whole httpModules element, because no XPath is needed for that.
回答1:
The XPath Locator expects a fully qualified XPath location, so your transform is not currently matching any elements. If you use the Condition Locator (which expects a relative XPath) instead, it should match:
<add xdt:Locator="Condition(starts-with(@type,'Elmah.')" xdt:Transform="RemoveAll"/>
Note also that the xdt:Transform Remove will only operate on the first matched element, so you need to use RemoveAll to achieve what you want.
The summary on msdn covers this quite well.
回答2:
Have you tried removing each module individually?
<add name="ErrorLog" xdt:Locator="Match(name)" xdt:Transform="Remove"/>
<add name="ErrorMail" xdt:Locator="Match(name)" xdt:Transform="Remove"/>
<add name="ErrorFilter" xdt:Locator="Match(name)" xdt:Transform="Remove"/>
来源:https://stackoverflow.com/questions/12166618/how-would-i-remove-elmah-modules-from-web-config-using-xml-document-transform