xdt transforms by a child node attribute

非 Y 不嫁゛ 提交于 2019-12-11 07:48:22

问题


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>


来源:https://stackoverflow.com/questions/54185439/xdt-transforms-by-a-child-node-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!