Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using xmlpeek in Nant script gives odd error

Tags:

nant

As part of a CI process I am trying to create a buildlabel which consists of the content of an xml element within an xml structure. For this purpose I am using nant and xmlpeek. My problem is that I get an odd error stating:

"Nodeindex '0' is out of range"

This is only the case if the xml file I am xmlpeeking contains a namespace definition in the root node.

Removing the namespace from the xml file gives me the output I expect.

The nant target that generates the error can be boild down to:

    <target name="TDSLabel">
            <property name="element" value=""/>
            <echo message="Getting element" />
            <xmlpeek file="C:\xxx\test1.xml" xpath="//Project/PropertyGroup/ProductVersion" property="element"/>
            <echo message="The found element value was: ${element}" />
    </target>

and the test1.xml file looks like this:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <ProductVersion>9.0.21022</ProductVersion>
    </PropertyGroup>
</Project>
like image 777
VilladsR Avatar asked Mar 20 '12 20:03

VilladsR


1 Answers

You already gave the right hint yourself. It's about the namespace. This should fix it:

<target name="TDSLabel">
  <property name="element" value=""/>
  <echo message="Getting element" />
  <xmlpeek
    file="C:\xxx\test1.xml"
    xpath="//x:Project/x:PropertyGroup/x:ProductVersion"
    property="element"
    verbose="true">
    <namespaces>
      <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
    </namespaces>
  </xmlpeek>
  <echo message="The found element value was: ${element}" />
</target>
like image 52
The Chairman Avatar answered Oct 21 '22 15:10

The Chairman