xdt通过子节点属性进行转换

时间:2019-01-14 16:27:22

标签: xml xdt-transform

我正在尝试创建一个转换,该转换将基于此answer移除其子节点,而不会成功

这样:

<?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>

将转换为:

<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>

这就是我到目前为止:

<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>

您可以在线使用transform simulator

1 个答案:

答案 0 :(得分:1)

最要注意的是,要删除的元素继承了默认名称空间xmlns="urn:schemas-microsoft-com:asm.v1"。也就是说,您尝试的XPath将不匹配任何内容,因为它没有考虑名称空间。

您可以忽略名称空间(不可行),或创建指向默认名称空间的前缀并在XPath中使用该前缀,例如:

<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>

我喜欢按照链接的答案中的建议使用Condition

<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>