This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: simple XPath question


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]