[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: simple XPath question

Subject: Re: simple XPath question
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 3 Jul 2001 09:39:21 +0100
xpath select position
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" />
</xsl:template>

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="." />
</xsl:template>

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]">
   ...
</xsl:template>

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:choose>
         <xsl:when test="position() = 1">
            <first-country>
               <xsl:value-of select="." />
            </first-country>
         </xsl:when>
         <xsl:otherwise>
            <country>
               <xsl:value-of select="." />
            </country>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:for-each>
</xsl:template>

Cheers,

Jeni

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


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.