xdt transforms by a child node attribute

2019-07-31 23:49发布

问题:

I'm trying to create transform that will remove a node by it child node based on this answer without success

so that:

<?xml version="1.0"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http1" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

will be transformed to:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">        
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http1" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

this is what I've so far:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly xdt:Transform="Remove" xdt:Locator="XPath(./assemblyIdentity[@name='System.Net.Http'])">
        <assemblyIdentity name="System.Net.Http" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

you can use this online transform simulator

回答1:

The most important thing to note is that the element that you want to remove inherits default namespace xmlns="urn:schemas-microsoft-com:asm.v1". That said, your attempted XPath will not match anything since it didn't account for the namespace.

You can either ignore the namespace (inadvisable), or create a prefix that point to the default namespace and use that prefix in your XPath, for example:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:pref="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly xdt:Transform="Remove" xdt:Locator="XPath(./pref:assemblyIdentity[@name='System.Net.Http']/..)">
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

I prefer using Condition as suggested in the linked answer:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:pref="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly xdt:Transform="Remove" xdt:Locator="Condition(pref:assemblyIdentity/@name='System.Net.Http')">
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>