Re: [xsl] Numbered Lists

Subject: Re: [xsl] Numbered Lists
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Tue, 9 Oct 2001 17:23:23 -0400
[Hewko, Doug]

> How can I generate a numbered list for a table of contents in XSL? I can
> generate and indent the table of contents, but I can't get the numbering
to
> work. I thought of using the count() function. That works nicely for the
> chapters in the contents, but fails for the chapter names in the document.
> (They should match.) I thought of using a variable, but that seems quite
> messy. Any suggestions?

Yes - use variables!!! They make your job very easy.  Before getting into
that, though, your html is not doing what you think. You are creating <a>
sections with no content which is unlikely to work and shows nothing
visible, and your HREF parts are not getting traversed at all.

Here is a stylesheet that does what you said you want, except it doesn't do
indenting or hrefs.  I can see that you can handle them, anyway.  This
example numbers things as you wanted:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method='html' />

<xsl:variable name='chapters' select='/root/chapter'/>

<xsl:template match="/">
<body>
 <xsl:for-each select='$chapters'>
     <xsl:variable name='chapnum' select='position()'/>
     <h3>
        <xsl:value-of select='$chapnum'/>&#160;
        <xsl:value-of select='name'/><br/>
     </h3>
     <xsl:variable name='sections' select='section'/>

     <xsl:for-each select='$sections'>
        <h4>
        <xsl:value-of select='$chapnum'/>.<xsl:value-of
           select='position()'/>&#160;
        <xsl:value-of select='name'/>
         </h4>
     </xsl:for-each>
 </xsl:for-each>
</body>
</xsl:template>

</xsl:stylesheet>

=====================================
Here is the result:

<body>
<h3>1

  Name of first chapter<br></h3>
<h4>1.1

    Chapter 1 section 1</h4>
<h4>1.2

    Chapter 1 section 2</h4>
<h3>2

  Name of second chapter<br></h3>
</body>

=============================================

This displays just as you want.

As an aside, your stylesheet selected on section/name and chapter/name, when
the thing you were really processing was a section or a chapter.  It's
better to select on the main thing you are processing, not a child of it.

Cheers,

Tom P

>
> My XML document is like this:
> <root>
> <chapter><name>Name of first chapter</name>
> <section><name>Chapter 1 section 1</name></section>
> <section><name>Chapter 1 section 2</name></section>
> <chapter><name>Name of second chapter</name>
> </root>
>
> I want my document to be like this:
> TABLE OF CONTENTS
> 1. Name of first chapter
>    1.1 Chapter 1 section 1
>    1.2 Chapter 1 section 2
> 2. Name of second chapter
>



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


Current Thread