具有命名空间的元素的XPath

具有命名空间的元素的XPath,xpath,msbuild,xml-namespaces,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" /> </dependentAs

元素中属性
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>
/configuration/runtime/assemblyBinding/dependentAssembly[2]/bindingRedirect[@newVersion='2.2.5.0']/@newVersion

在XSLT1.0中,为了能够在XPath中使用名称空间,必须用前缀声明名称空间

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


但是,您不必指定整个路径,您可以使用快捷方式:

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

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


快捷方式可能会选择不同的节点。可能不在这里,因为它看起来像一个默认的应用程序配置文件,但通常我会避免使用快捷方式。问题被标记为XSLT,但我在问题中没有看到任何东西表明它实际上是关于XSLT的。@ThomasW Correct,我只是想概述一下,一般来说,没有必要使用过度特定的路径。@JLRishe我信任该标记。XSLT并不是一个容易放错位置的标签,IMHO。很抱歉XSLT的粉丝们。我正在尝试使用MSBUILD中的TransformXml任务转换app.config。别客气。那么,关于如何做到这一点,有什么答案吗?每个人似乎都在给我评论,似乎没有人知道答案。我想不出解决办法。这就是我在问的问题。我并不是在说自己是天才。+1感谢分享解决方案和解释。下次提示:如果您没有实际使用XSLT,请不要使用XSLT标记。
 <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)"/>