Re: [xsl] Counting Footnotes

Subject: Re: [xsl] Counting Footnotes
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 21 Sep 2001 11:22:37 +0100
Hi Nicholas,

> I have some XML which contains footnotes, these are in the style
> <footnote>something</footnote> they can appear anywhere in the
> source tree. I can create a link to them no problem (the documents
> root element I push twice, once for the main body, and again with a
> mode="footnote" to write out the footnotes out at the bottom of the
> document. However, footnotes are usually numbered, I would like to
> be able to number each footnote, in the text, and place the
> corresponding number at the bottom of the text next to the footnote.

Use xsl:number with level="any" to number the footnotes in the main
text, e.g.:

<xsl:template match="footnote">
  <a href="..."><xsl:number level="any" /></a>
</xsl:template>

With level="any", xsl:number numbers nodes according to their position
amongst any of the same type of (and name of) node within the document.

However, when you're actually outputting the footnotes, it will be
more efficient if you apply templates directly to all the footnotes
at once and use their position to number them:

<xsl:template match="/*" mode="footnote">
  <xsl:apply-templates select="//footnote" mode="footnote" />
</xsl:template>

<xsl:template match="footnote" mode="footnote">
  <a name="..."><xsl:value-of select="position()" /></a>
  <xsl:apply-templates />
</xsl:template>

This is because xsl:number forces the XSLT processor to look at all
the preceding and ancestor nodes of each footnote - a lot of
traversals. Collecting all the footnotes together and going through
them in sequence saves the processor scrabbling all over the tree.

You can use the value attribute of xsl:number to format the position()
in the same way as you use it to format the number it generates, e.g.:

  <xsl:number level="any" format="[a]" />
  ...
  <xsl:number value="position()" format="[a]" />

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread