xdt 通过子节点属性转换 [英] xdt transforms by a child node attribute

查看:28
本文介绍了xdt 通过子节点属性转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建转换,该转换将根据此 回答 没有成功

这样:

<配置><运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><依赖程序集><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><依赖程序集><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></运行时></配置>

将转换为:

<预><代码><配置><运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><依赖程序集><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></运行时></配置>

这是我到目前为止:

<配置xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"><运行时><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></运行时></配置>

您可以在线使用此转换模拟器

解决方案

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

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

<配置xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"><运行时><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></运行时></配置>

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

<配置xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"><运行时><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></运行时></配置>

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

解决方案

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>

这篇关于xdt 通过子节点属性转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆