xslt passing position as a parameter is producing strange results?

I have an xml file which looks like this:

  <title> title of section </title>
     <title> title of table</title>
     <tableData columns = 2>
     <column width ="50"/>
     <column width ="100"/>
             <entry>column 1</entry>
             <entry>column 2</entry>

I need to process each entry in tableHead/row/entry and spit it out in some format that also includes the columnWidth. So, my output must look like {"name":"column 1", "width":"50px"},{"name":"column 2", "width":"100px"}

My transform looks like this

 <xsl:template match="//table/tableHead/row/entry">
    <xsl:text>{"name": "</xsl:text><xsl:value-of select"."/>
    <xsl:text>", "width":"</xsl:text>

    <xsl:call-template name="get_column_width">
       <xsl:with-param name ="rowNum">
          <xsl:value-of sellect"position()"/>

 <xsl:template name ="get_column_width">
     <xsl:value-of select="../../../columnWidth[$rowNum]/@width"/>

Strangely, this produces {"name":"column 1", "width":"50 100px"},{"name":"column 2", "width":"50 100px"}

However, when I output only the rowNum parameter I get exactly what I expect:

{"name":"column 1", "width":1px"},{"name":"column 2", "width":"2px"}

So, rowNum is 1 and 2 so the select statement will use columnWidth[1] and columnWidth[2], but for whatever reason it just outputs both of them. I tried using columnWidth[1] in the select and it outputs 50 as I would expect. columnWidth[2] outputs 100. I'm really confused here. Using preceding-sibling gave the same result. Is there something wrong with my template matching?

Unfortunately changing the xml format is not an option.


Instead of:

<xsl:value-of select="../../../columnWidth[$rowNum]/@width"/>


<xsl:value-of select="../../../columnWidth[position() = $rowNum]/@width"/>


In XPath:


is a shorthand for:

someExpression[position() = someInteger]

However, in case of:


the XPath 1.0 processor typically doesn't know what value and of what type is contained in the variable. Therefore, it converts this variable to a boolean value (true() or false()) to perform the complete evaluation.

By definition:


is equivalent to:

not(somenumber = 0)

As neither of the numbers 1 and 2 is zero, the predicate [$rowNum] is true() in both cases,



selects the width attributes of both columnWidth elements.

The value of $rowNum is a result tree fragment (or in XSLT 2.0, a document node). The effective boolean value of a result tree fragment (or a node) is always true. You probably imagined that the value was an integer. But to set a variable to an integer, you should write

<xsl:with-param name="x" select="position()"/>


<xsl:with-param name="x">
  <xsl:value-of select="position()"/>

Sadly the latter construct seems to be incredibly prevalent, considering how verbose and inefficient it is, and how occasionally (as here) it gives the wrong answer.

Need Your Help

phonegap / iphone / zxing

iphone cordova zxing

I need to add Zxing (a qrcode reader app) to my iphone app, made in html, css &amp; js, phonagap compiled.

Planewidth after bending (using pv3d, as3dmod)

width papervision3d plane vertices

I got a huge problem. I'm stuck there for two weeks now.