Re: [xsl] simple XPath question

Subject: Re: [xsl] simple XPath question
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 3 Jul 2001 09:39:21 +0100
Hi Chris,

> And the position() values are really a separate thing entirely. They
> are positions in the source tree, not in the node-set, since nodes
> in node-sets don't have positions.
> (Do I have this right?)

Almost. You're right that nodes in node sets don't have positions
(since node sets are by definition unordered), but nodes *can* have
positions in the context node list. How position() works depends on
whether you are using it in a *pattern* (in a match attribute) or in
an expression (in a select or test attribute).

When you *select* or *test* nodes with an expression, then position()
gives the position of the context node in the context node list.  If
you select nodes to apply templates to:

<xsl:template match="warehouse">
  <xsl:apply-templates select="item/country" />

then within the template for those nodes, the current node list
contains all the country children of the item children of the
warehouse element.  The current node is the country element that
you're looking at in the current node list.  So if you do:

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

Then you will get a sequentially numbered list of countries.

When you *match* nodes with a pattern, then position() gives the
position of the node amongst its similar siblings - it always uses the
source tree to work out the position because match patterns have no
concept of context node lists.  So as you've seen, if you do:

<xsl:template match="country[1]">

then this template will match any country element that is the first
country element child of its parent.

Personally, I try to avoid using position() in templates because it's
easy to lose track of what the current node list holds; if I need to
use position() then I usually use xsl:for-each to iterate over the
nodes rather than applying templates to them, so:

<xsl:template match="warehouse">
   <xsl:for-each select="item/country">
         <xsl:when test="position() = 1">
               <xsl:value-of select="." />
               <xsl:value-of select="." />



Jeni Tennison

 XSL-List info and archive:

Current Thread