Using xmlpeek in Nant script gives odd error
Asked Answered
W

2

7

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>
Wilbur answered 20/3, 2012 at 20:5 Comment(0)
M
6

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>
Matrilocal answered 21/3, 2012 at 17:48 Comment(1)
As stated below the same problem might occur when using xmlpoke ( #2585266). Strangely I didn't get any google hits directly on the errormessage combined with neither xmlpeek or nant. Thanks for your answer, now other people might not get stuck with that odd errormessageWilbur
W
0

Found a similar problem and the anwser to my problem here: XmlPoke and unique nodes. The problem was that I did not include the namespace definition within the xmlpeek element and afterwards omitted the necessary reference to the namespace in my xpath statement:

<xmlpeek file="C:\xxx\test1.xml" xpath="//x:Project/x:PropertyGroup/x:ProductVersion" property="element">
    <namespaces>
        <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
    </namespaces>
</xmlpeek>
Wilbur answered 21/3, 2012 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.