This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: a nicer total sibling count than this
- From: "Michael Kay" <michael dot h dot kay at ntlworld dot com>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Thu, 19 Sep 2002 15:33:35 +0100
- Subject: RE: [xsl] a nicer total sibling count than this
- Reply-to: xsl-list at lists dot mulberrytech dot com
> To my surprise and delight, the following worked, producing
> heavy def numbers with a following non-break space:
>
> <xsl:template match="def">
> <xsl:if test="(count(preceding-sibling::def) +
> count(following-sibling::def)) >= 1">
> <xsl:element name="strong">
> <xsl:number/>
> <xsl:text> </xsl:text>
> </xsl:element>
> </xsl:if>
> <xsl:apply-templates/>
> </xsl:template>
>
> Is there a prettier/more efficient way of doing the same
> thing without resorting to frankly ugly
> '(count(preceding-sibling::def)+count(following-sibling::def))'?
>
You've had a number of solutions to this, but there is another. Assuming
this template rule is processed by <xsl:apply-templates select="def"/>
or equivalent, you can write
<xsl:template match="def">
<xsl:if test="last()!=1">
<strong><xsl:value-of select="concat(position(),
Š)"/></strong>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>
Using position() and last() will often be more efficient than using
count() and xsl:number, because they work on the current node list,
which you are using anyway to output the nodes.
Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list