This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: figuring out if first or last child
- From: Peter Davis <pdavis152 at attbi dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Thu, 28 Mar 2002 21:37:38 -0800
- Subject: Re: [xsl] figuring out if first or last child
- References: <20020328.231841.-797907.4.eric_k_taylor@juno.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
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