2

What is the XPATH for attribute newVersion in the element

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

I have tried my best to do it by myself. But don't know how to get XPATH for elements with namespace. Its very confusing. Somebody please provide me a XPATH.

XPATH which I came up with is

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

Tomalak
  • 332,285
  • 67
  • 532
  • 628
jero2rome
  • 1,548
  • 1
  • 21
  • 39

2 Answers2

5

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)"/>
jero2rome
  • 1,548
  • 1
  • 21
  • 39
  • 2
    +1 for sharing the solution along with an explanation. Hint for next time: Don't use the XSLT tag if you are not actually working with XSLT. – Tomalak Sep 19 '14 at 09:12
2

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
" />
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • The shortcut could potentially select different nodes. Maybe not here, since it looks like a default application configuration file, but in general I'd avoid shortcuts. – Thomas Weller Sep 16 '14 at 13:02
  • The question is tagged as XSLT, but I don't see anything in the question to suggest that it's actually about XSLT. – JLRishe Sep 16 '14 at 13:03
  • @ThomasW Correct, I just wanted to outline that an over-specific path is not necessary in general. – Tomalak Sep 16 '14 at 13:06
  • 1
    @JLRishe I trusted the tag. XSLT is not a tag that's easy to misplace, IMHO. – Tomalak Sep 16 '14 at 13:07
  • Sorry for XSLT fans. I'm trying to tranform the app.config using TransformXml task in MSBUILD. Din't mention it. So, Any answer on how to do this? Everybody seems to give me comments and nobody seems to know the answer. I couldn't come up with the solution. Thats y I'm asking here. I'm not claiming to be genius. – jero2rome Sep 16 '14 at 16:04