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

Re: Reference a counter

Subject: Re: Reference a counter
From: Mike Brown <mike@xxxxxxxx>
Date: Mon, 13 May 2002 13:06:47 -0600 (MDT)
counter plays football
Early, Mary wrote:
> Is there any way to provide a reference to a counter value. For example, we
> typically use a superscripted number to reference a table note at the bottom
> of the table. How can we grab the counter value of the table note and place
> it in the cell as a cross reference.

The word 'counter' in your message sets off FAQ alarms. Until XSLT/XPath 2.0
becomes commonplace, counters are best left to procedural languages, not
functional, declarative languages like XSLT. In XSLT, problems involving
incrementing an arbitrary value or computing subtotals tend to have similar
solutions. They generally involve some creative thinking as to how you can
calculate the counter or subtotal you need, without resorting to using a
counter variable at all.

In your case, footnotes very similar to generating a table of contents, and
these tasks involve the same kind of approach as the counter/subtotal problem.
You should set up your stylesheet such that it makes two passes through the
parts of your source document that generate the references. You can do this
with modes. You can also use different methods to calculate the
footnote/reference numbers when the time comes to generate them.

Given XML:
<?xml version="1.0" encoding="utf-8"?>
<data>
  <record>
    <name>Jane Doe</name>
    <note>Likes to play guitar</note>
  </record>
  <record>
    <name>John Public</name>
    <note/> <!-- empty note -->
  </record>
  <record>
    <name>Charlie Brown</name>
    <note>Plays football</note>
  </record>
</data>

...and wanting to produce this output:

  1. Jane Doe [1]
  2. John Public
  3. Charlie Brown [2]
 --------------------------------------
 [1] Likes to play guitar
 [2] Plays football

...your stylesheet would be:


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates select="data/record"/>
    <xsl:text> --------------------------------------&#10;</xsl:text>
    <xsl:apply-templates select="data/record/note[normalize-space()]" mode="footnotes"/>
  </xsl:template>

  <xsl:template match="record">
    <xsl:value-of select="concat(' ',position(),'. ',name)"/>
    <xsl:apply-templates select="note[normalize-space()]"/>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="note">
    <!-- at this point, the current node list only has one note in it,
         so we have to look back up the source tree to find out what
         number it should be -->
    <xsl:value-of select="concat('[',count(preceding::note[normalize-space()])+1,']')"/>
  </xsl:template>

  <xsl:template match="note" mode="footnotes">
    <!-- at this point, the current node list contains the non-empty notes,
         so we can simply use position() to determine the current note's
         number -->
    <xsl:value-of select="concat('[',position(),'] ',.,'&#10;')"/>
  </xsl:template>

</xsl:stylesheet>


This, of course, is just one of several ways to go about it. It should give
you an idea of how careful selection of nodes to process, use of the position
function, and examining the source tree can all help you get away from the 
notion that you need a counter.

   - Mike
____________________________________________________________________________
  mike j. brown                   |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  resume: http://skew.org/~mike/resume/

 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.