xsl-list
[Top] [All Lists]

Re: [xsl] position() outside for-each

2007-11-14 03:21:01
On 13/11/2007, Scott Trenda <Scott(_dot_)Trenda(_at_)oati(_dot_)net> wrote:
David,

I'm guessing that his stylesheet had a generic <xsl:apply-templates/>,
and he was getting 2, 4, 6, 8, etc... the position including the
whitespace text nodes.

Charles,

Use xsl:number, it'll usually give you exactly what you're looking for.
:)


It's worth adding to that to say that position() gives you the
position of the current node within the list of selected nodes to
process.

So as Scott says, if you have <xsl:apply-templates/> then the select
nodes to process will most likely be:

whitespace node, element, whitespace node, element etc

...so the positions of the elements in that list are 2, 4 and so on
(with the whitespace nodes being 1 and 3)

The thing to remember is that the number returned by position() is
controlled by the stylesheet writer, not by the input.  For example if
you selected the nodes to process using:

select="mynode[2]"

position() would return 1

The other thing I've seen done is:

<xsl:for-each select="/root/item">
  <xsl:apply-templates select="."/>
</xsl:for-each>

Here position() would always return 1 within the item matching
template, because instead of a single list of nodes to process (where
each <item> has a position within that list) there are lots of lists
(equal to the number of <item>s) with a single <item> in each.


cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>