Re: Position() question

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

>, 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">
  <head>
    <xsl:apply-templates select="text" />
  </head>
</xsl:template>

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

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">
  <item>
    <label><xsl:number /></label>
    <text><xsl:value-of select="." /></text>
  </item>
</xsl:template>

I hope that this helps,

Jeni

Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread