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

Interesting numbering problem

Subject: Interesting numbering problem
From: Joerg Pietschmann <joerg.pietschmann@xxxxxx>
Date: Wed, 11 Apr 2001 14:35:55 +0200
 Interesting numbering problem
Hello all,
here is an interesting problem that my students presented me.

Take this simple FAQ XML file:

<faq>
  <qa>
    <q>Sample question.</q>
    <a>Sample answer.</a>
  </qa>
  <qa>
    <q>A <keyword>hairy</keyword> question.</q>
    <a>Another answer.</a>
  </qa>
</faq>

This should be transformed into text, with a keyword index.
The question should be numbered, both in normal text and in
the keyword index, and the questions should be sorted
(alphabetically, just to have a simple example):


Q1: A hairy question.
A: Another answer.
===================

Q2: Sample question.
A: Sample answer.
===================
hairy: question 1

It took me a while to find a XSL which generates this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="ASCII"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="faq">
    <xsl:apply-templates><xsl:sort select="q"/></xsl:apply-templates>
    <xsl:apply-templates select="//keyword" mode="keywordindex"/>
  </xsl:template>

  <xsl:template match="qa">
    <xsl:text>
Q</xsl:text>
    <xsl:value-of select="position()"/>
    <xsl:text>: </xsl:text>
    <xsl:apply-templates select="q"/>
    <xsl:text>
A: </xsl:text>
    <xsl:apply-templates select="a"/>
    <xsl:text>
===================
</xsl:text>
  </xsl:template>

  <xsl:template match="keyword" mode="keywordindex">
    <xsl:value-of select="."/>
    <xsl:text>: question </xsl:text>
    <xsl:variable name="q" select="generate-id(ancestor::qa)"/>
    <xsl:for-each select="//qa">
      <xsl:sort select="q"/>
      <xsl:if test="generate-id()=$q">
        <xsl:value-of select="position()"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>

The method for generating the question number in the keyword index appears
to be somewhat complicated and is a potential performance drain. Is there
something easier? The sorting defeats using 1+count(ancestor::qa/preceding::qa)
for the question number in the keyword index generation template (or am i
wrong?).
(i have the feeling i'll get the recommendation to build a variable with
the sorted qa-elements and work on that, using node-set() :-)

J.Pietschmann

 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.