A FAQ. You are counting the whitespace text nodes as well as the
elements. Use <xsl:strip-space> to get rid of the text nodes.
Michael Kay
-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of
DURDINA Michal
Sent: 09 May 2003 16:52
To: 'xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com'
Subject: [xsl] position() return's only even numbers for
nested elements
Hello,
please take a look at this xsl fragment, maybe you find a
simple explanation to strange behaviour of position()
function for nested elements.
Consider this input xml:
<uml>
<package id="1"/>
<package id="2"/>
<package id="3">
<package id="31"/>
<package id="32"/>
</package>
<package id="4">
<package id="41">
<package id="411"/>
<package id="412"/>
<package id="413"/>
</package>
</package>
<package id="5"/>
</uml>
Now I want to print position of every single 'package' element:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="uml">
<result>
<xsl:apply-templates select="package"/>
</result>
</xsl:template>
<xsl:template match="package">
<xsl:copy>
<xsl:copy-of select="@id"/>
<xsl:attribute name="position"><xsl:value-of
select="position()"/></xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And the result under Saxon and Xalan is:
<result>
<package id="1" position="1"/>
<package id="2" position="2"/>
<package id="3" position="3">
<package id="31" position="2"/>
<package id="32" position="4"/>
</package>
<package id="4" position="4">
<package id="41" position="2">
<package id="411" position="2"/>
<package id="412" position="4"/>
<package id="413" position="6"/>
</package>
</package>
<package id="5" position="5"/>
</result>
Why all nested 'package' element have even positions (2, 4,
6, ...) but top-level 'package' elements positions behaves
right (1, 2, 3,...) ???
When I replace <xsl:apply-templates/> with
<xsl:apply-templates select="packages"/>, then it works right
also for nested 'packages' elements.
I''ll be thankful for every explanation.
Michal
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list