带有命名空间的元素的XPath

时间:2014-09-16 11:31:47

标签: xpath msbuild xml-namespaces

元素

中属性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>

2 个答案:

答案 0 :(得分: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
" />

答案 1 :(得分:2)

正确的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)"/>