Re: Position() question

Subject: Re: Position() question
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 01 Sep 2000 19:09:18 +0100

>, but only if I strip whitespace so that position() doesn't pick it up
>whitespace nodes.
>How can I preserve whitespace and still output the correct label number,
>without resorting to: <xsl:value-of select="count(preceding-sibling::text +
>1)" /> ?

position() gives you the index of the current node within the current node
list.  So one way to do it is to make the current node list be the list
that you need in order to give the position() that you want.

You want to number the 'text' elements with respect to each other.  With
position(), you can do this by making sure the current node list only
contains 'text' elements.  In other words, by selecting only those elements
for processing:

<xsl:template match="head">
    <xsl:apply-templates select="text" />

<xsl:template match="text">
    <label><xsl:value-of select="position()" /></label>
    <text><xsl:value-of select="." /></text>

Another thing that you can do is to use xsl:number, which is specially
designed to give you numbering in your output.  Probably a more elegant and
transparent solution than using position() is to use xsl:number.  Without
any attributes, it will give you the index of the 'text' element amongst
its 'text' element siblings:

<xsl:template match="text">
    <label><xsl:number /></label>
    <text><xsl:value-of select="." /></text>

I hope that this helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread