【问题标题】:XPath for element with namespace具有命名空间的元素的 XPath
【发布时间】:2014-09-19 17:13:07
【问题描述】:

元素中属性newVersion的XPATH是什么

<dependentAssembly>
<assemblyIdentity name="System.Reactive.Linq" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
</dependentAssembly> 

我已经尽力自己做。但不知道如何为具有命名空间的元素获取 XPATH。它非常混乱。有人请给我一个 XPATH。

我想出的XPATH是

/configuration/runtime/assemblyBinding/dependentAssembly[2]/bindingRedirect[@newVersion='2.2.5.0']/@newVersion


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="1" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Reactive.Interfaces" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect name="Test1" oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Reactive.Linq" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
  </dependentAssembly>      
</assemblyBinding>

【问题讨论】:

    标签: xpath msbuild xml-namespaces


    【解决方案1】:

    正确的xpath是

    XPATH:

    /configuration/runtime/ns:assemblyBinding/ns:dependentAssembly[ns:assemblyIdentity[@name='System.Reactive.Linq']]/ns:bindingRedirect/@newVersion

    其中ns 是命名空间urn:schemas-microsoft-com:asm.v1

    我在项目文件的 MSBuild 任务中使用 XmlPoke 任务来修改绑定重定向。 与 XmlPoke 任务一起,代码如下所示:

     <XmlPoke XmlInputPath="$(DestXmlFiles)" 
              Namespaces="&lt;Namespace Prefix='ns' Uri='urn:schemas-microsoft-com:asm.v1' Name='DoNotKnowWhatThisIsFor-ButItIsRequired' /&gt;"
              Query="/configuration/runtime/ns:assemblyBinding/ns:dependentAssembly[ns:assemblyIdentity[@name='System.Reactive.Linq']]/ns:bindingRedirect/@newVersion"
              Value="$(BUILD_NUMBER)"/>
    

    【讨论】:

    • +1 用于分享解决方案和解释。下次提示:如果您实际上并未使用 XSLT,请不要使用 XSLT 标记。
    【解决方案2】:

    在 XSLT 1.0 中,您必须声明带有前缀的命名空间,以便能够在 XPath 中使用它们。

    例如(为便于阅读而包装):

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:asm="urn:schemas-microsoft-com:asm.v1"
    >
      <xsl:template match="/">
        <xsl:value-of select="
          /configuration/
           runtime/
           asm:assemblyBinding/
           asm:dependentAssembly[2]/
           asm:bindingRedirect[@newVersion = '2.2.5.0']/@newVersion
         " />
      </xsl:template>
    </xsl:stylesheet>
    

    但是,您不必指定整个路径,您可以走捷径:

    <xsl:value-of select="
      //asm:assemblyIdentity[@name='System.Reactive.Linq']/
        asm:bindingRedirect[@newVersion = '2.2.5.0']/@newVersion
    " />
    

    【讨论】:

    • 快捷方式可能会选择不同的节点。也许不在这里,因为它看起来像一个默认的应用程序配置文件,但总的来说我会避免使用快捷方式。
    • 该问题被标记为 XSLT,但我看不出问题中的任何内容表明它实际上与 XSLT 有关。
    • @ThomasW 正确,我只是想概述一下通常不需要过度特定的路径。
    • @JLRishe 我信任这个标签。 XSLT 不是一个容易放错位置的标签,恕我直言。
    • 对 XSLT 粉丝感到抱歉。我正在尝试使用 MSBUILD 中的 TransformXml 任务转换 app.config。不提了。那么,关于如何做到这一点的任何答案?似乎每个人都给我 cmets,但似乎没有人知道答案。我想不出解决办法。那就是我在这里问。我不是自称天才。
    猜你喜欢
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多