RE: [xsl] problem with proper counting

Subject: RE: [xsl] problem with proper counting
From: "Chris Bayes" <chris@xxxxxxxxxxx>
Date: Wed, 24 Oct 2001 09:31:06 +0100
Andreas,
Beginners rule #1. Don't use xsl:for-each.
The following stylesheet gives examples of what you want.
<xsl:template match="page">
	<p>
		<a>
			<xsl:attribute name="name"><xsl:number
level="multiple" format="1-1" count="page|set"/></xsl:attribute>
			<xsl:value-of select="@id" />
		</a>
		<a>
			<xsl:attribute name="href">#<xsl:call-template
name="prep" /></xsl:attribute>
			Previous Page
		</a>
		<a>
			<xsl:attribute name="href">#<xsl:call-template
name="nexp" /></xsl:attribute>
			Next Page
		</a>
	</p>
</xsl:template>
<xsl:template name="prep">
	<xsl:param name="e" />
	<xsl:for-each select="preceding::page[1]">
		<xsl:number level="multiple" format="1-1"
count="page|set"/>
	</xsl:for-each>
</xsl:template>
<xsl:template name="nexp">
	<xsl:param name="e" />
	<xsl:for-each select="following::page[1]">
		<xsl:number level="multiple" format="1-1"
count="page|set"/>
	</xsl:for-each>
</xsl:template>

Notice that I do use xsl:for-each so you might think that I am
contradicting beginners rule #1 but I'm not exactly. Rule #1 states in
section 2 that xsl:for-each is mainly used to change context. That is
what I am doing here. Xsl:number calculates the result from the current
element. When the template prep is called the for-each selects the first
preceding page element so that xsl:number calculates it's result from
the context of the preceding page element. The same for nexp but using
the context of the following page element.

Ciao Chris

XML/XSL Portal
http://www.bayes.co.uk/xml



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


Current Thread