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

RE: Producing a Table of Contents

Subject: RE: Producing a Table of Contents
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Fri, 26 Mar 2004 03:43:48 -0700
xsl table of contents
Using the mode attribute of apply-templates allows you to select
particular elements and transform them different for each type of mode.
Using "//hi" will select all h1 elements from your source.  Using
mode="toc" allows you to match each of these elements to the template
with the match attribute set to "h1" and the mode set to "toc".

So this XML...

<?xml version="1.0"?>
<html>
    <h1>this is heading 1</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
    <h1>this is heading 2</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
    <h1>this is heading 3</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
    <h1>this is heading 4</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i></b><i> with italicized text</i></p>
  </body>
</html>

Transformed by this XSL...

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="html"/>
<xsl:template match="html">
Table of Contents: <br/>
 <xsl:element name="ol">
  <xsl:apply-templates select="//h1" mode="toc"/>
 </xsl:element>
 <xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="h1 | p | b | i">
  <xsl:copy>
    <xsl:element name="name()">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:copy> 
</xsl:template>

<xsl:template match="h1" mode="toc">
  <xsl:element name="li">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

Gives you this output:

Table of Contents: <br>
<ol>
  <li>this is heading 1</li>
  <li>this is heading 2</li>
  <li>this is heading 3</li>
  <li>this is heading 4</li>
</ol>
<h1>this is heading 1</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 2</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 3</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 4</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>

Best of luck!

<M:D/>

-----Original Message-----
From: Julian Voelcker [mailto:asp@xxxxxxx] 
Sent: Friday, March 26, 2004 2:56 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Producing a Table of Contents

When doing a transformation, is there any 'clever' way of generating a 
table of contents in the XSLT based on the actual content of the 
template?

For example, the template has lots of sections of text preceded with 
headings (using <h1></h1> tags).  Using XSLT is it possible to search 
through the template to draw up a list of the text between these tags?
-- 
Cheers,

Julian Voelcker
United Kingdom

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.