XPath for element with namespace

主宰稳场 提交于 2019-12-02 10:48:54

In XSLT 1.0 you must declare namespaces with a prefix in order to be able to use them in XPath.

For example (wrapped for legibility):

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

However, you don't have to specify the entire path, you could take shortcuts:

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

The right xpath is

XPATH:

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

Where ns is the namespace urn:schemas-microsoft-com:asm.v1

I use a XmlPoke Task in the MSBuild tasks in the project file to modify the binding redirect. Together with a XmlPoke Task the code goes like this:

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