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

Re: figuring out if first or last child

Subject: Re: figuring out if first or last child
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Thu, 28 Mar 2002 21:37:38 -0800
xsl position last first
On Thursday 28 March 2002 20:08, Eric K Taylor wrote:
> <xsl:if test="[this is the first article]">...
> and possibly for another issue
> <xsl:if test="[this is the last article]">...

The position() and last() functions can help you with this: when call 
<xsl:apply-templates/>, it creates a node-set containing all the children in 
the current element.  This node-set has a size (the number of elements in the 
set), which is returned by the last() function.  As you process the nodes in 
the set, the position() function returns the position of each node.


The easiest way to do this would be to have different templates that use 
position() and last() in their match rule:


<xsl:template match="newsletter">
  <!-- select all of the child articles and put them in a node-set.  This 
makes the last() function returns the number of articles. -->
  <xsl:apply-templates select="article"/>
</xsl:template>

<xsl:template match="article[position() = 1]">
  <!-- do something special for the first article -->
  <xsl:call-template name="common-article-stuff"/>
  <!-- more special stuff here -->
</xsl:template>

<xsl:template match="article[position() = last()]">
  <!-- do something special for the last article -->
  <xsl:call-template name="common-article-stuff"/>
  <!-- more special stuff here -->
</xsl:template>

<!-- all other articles -->
<xsl:template match="article">
  <!-- optionally do something special here, or combine this template with 
the common-article-stuff template (it is possible for a template to have both 
a name and a match attributes -->
  <xsl:call-template name="common-article-stuff"/>
</xsl:template>

<!-- code that is the same between all of the articles -->
<xsl:template name="common-article-stuff">
  <!-- do some stuff, like <xsl:apply-templates/> -->
</xsl:template>

-- 
Peter Davis
No guest is so welcome in a friend's house that he will not become a
nuisance after three days.
		-- Titus Maccius Plautus

 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.