Project

Profile

Help

Return value of position() is incorrect

Added by Anonymous over 19 years ago

Legacy ID: #3037208 Legacy Poster: Greg Mellinger (mellingg)

In my parent template rule I use: <xsl:apply-templates/> In my child template rule I use: <xsl:if test="position()=1">foo</xsl:if> which never fires. If I output the value of position() prior to my if test I receive: 4 6 8 10 ... why? If I modify my parent template rule: <xsl:apply-template select="child"/> My if test fires correctly on the first occurrence...why? I am using Saxonb8-1-1 on UNIX.


Replies (4)

Please register to reply

RE: Return value of position() is incorrect - Added by Anonymous over 19 years ago

Legacy ID: #3037223 Legacy Poster: Greg Mellinger (mellingg)

Also, If I create 2 child template rules: select="child[1]" and the other select="child[position()>1]" it fires correctly...why? Is this an issue with XSLT, Saxon, or my code?

RE: Return value of position() is incorrect - Added by Anonymous over 19 years ago

Legacy ID: #3037320 Legacy Poster: Michael Kay (mhkay)

A well-known gotcha. <xsl:apply-templates/> processes all the children of an element. Typically you will have element children at positions 2, 4, 6..., and whitespace text nodes separating the elements at positions 1, 3, 5.... If you write a template rule to process whitespace text nodes, rather than handing them to the built-in template, you will find that they are indeed numbered 1,3,5. If you process only the child elements, and not the whitespace text nodes, then the elements are numbered within the sequence of elements: 1,2,3. Alternatively, use xsl:strip-space to remove the whitespace text nodes from your document at source. Michael Kay

RE: Return value of position() is incorrect - Added by Anonymous over 19 years ago

Legacy ID: #3037323 Legacy Poster: Michael Kay (mhkay)

Within a predicate, such as item[1], you are selecting the first child element named "item". If you wrote node()[1] then you would select the first child node, which in your case is a whitespace text node. The default for xsl:apply-templates is to process all child nodes, not just the element children. Michael Kay

RE: Return value of position() is incorrect - Added by Anonymous over 19 years ago

Legacy ID: #3038728 Legacy Poster: Greg Mellinger (mellingg)

Thanks Mike. Rookie mistake. Respectfully, Greg

    (1-4/4)

    Please register to reply