Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2009, 11:34 PM
Authorized User
 
Join Date: Feb 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to number with letters & numbers

Hi everyone,

I'm trying to add numbering to a xml document that is organized like a book. Numbering chapters and chapter sections/subsections is fairly obvious, but I also have appendices with sections and subsections. I want to be able to number it like A.1, A.1.1, A.2, etc., but the xslt doesn't seem to like combining the letter with the numbers. For example, here is what I'm trying:

Code:
<appendix>
  <title>Appendix A Title</title>
  <sect1>
    <title>Section 1 Title</title>
    <para>Some content.</para>
    <sect2>
      <title>Subsection 1 Title</title>
      <para>Some more content.</para>
    </sect2>
  </sect1>
</appendix>
I would like this to be numbered like:
Appendix A Appendix A Title
A.1 Section 1 Title
A.1.1 Subsection 1 Title

The xsl:number code I'm using is this (this is all in a nested xsl:for-each to get the titles):

Code:
<xsl:text>Appendix </xsl:text><xsl:number format="A " level="multiple"/>
 
<xsl:number format="1 " level="multiple" count="appendix|sect1"/>
 
<xsl:number format="1 " level="multiple" count="appendix|sect1|sect2"/>
This will number the appendix title fine, but replaces the appendix letter with a number on the sect1 and sect2 elements. So it ends up looking like this:
Appendix A Appendix A Title
1.1 Section 1 Title
1.1.1 Subsection 1 Title

Any idea what I'm doing wrong? Any help would be appreciated.


Thanks,
Josh

Last edited by JoshC; February 24th, 2009 at 11:51 PM..
 
Old February 25th, 2009, 12:27 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

Try this:
Code:
 
<xsl:template match="appendix">
<xsl:text>Appendix </xsl:text><xsl:number format="A " level="multiple"/>
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>

<xsl:template match="sect1">
<xsl:number format="A" level="single" count="appendix"/>.<xsl:number format="1" level="multiple"/>
<xsl:value-of select="title"></xsl:value-of>
<xsl:text> </xsl:text>
<xsl:number format="A" level="single" count="appendix"/>.<xsl:number format="1" level="multiple" count="appendix|sect1|sect2"/>
<xsl:text> </xsl:text>
<xsl:value-of select="./sect2/title"></xsl:value-of>
</xsl:template>

__________________
Rummy
 
Old February 25th, 2009, 12:36 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Or try this. This would give a better result.

Code:
 
<xsl:template match="appendix">
<xsl:text>Appendix </xsl:text><xsl:number format="A " level="multiple"/>
<xsl:value-of select="title"></xsl:value-of>
<xsl:apply-templates select="sect1"></xsl:apply-templates>
</xsl:template>

<xsl:template match="sect1">
<xsl:number format="A" level="single" count="appendix"/>.<xsl:number format="1" level="multiple"/>
<xsl:value-of select="title"></xsl:value-of>
<xsl:apply-templates select="sect2"></xsl:apply-templates>
</xsl:template>
<xsl:template match="sect2">
<xsl:number format="A" level="single" count="appendix"/>.<xsl:number format="1" level="multiple" count="sect1|sect2"/>
<xsl:text> </xsl:text>
<xsl:value-of select="title"></xsl:value-of>
</xsl:template>

__________________
Rummy
 
Old February 25th, 2009, 04:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can either use multiple xsl:number calls as Rummy suggests, or better, use


<xsl:number level="multiple"
format="A.1.1"
count
="appendix|sect1|sect2"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 25th, 2009, 12:11 PM
Authorized User
 
Join Date: Feb 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Excellent solutions. Thank you!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare numbers and letters in same cell EricB123 Excel VBA 1 January 21st, 2007 03:30 PM
Remove letters from numbers Corey Access 7 December 18th, 2005 09:09 PM
fillin array with letters from a to Z and numbers sajid C# 10 May 3rd, 2005 03:38 PM
Number and letters (alphanumeric values) jorluiseptor Classic ASP XML 0 April 22nd, 2005 03:07 PM
Taking both numbers and letters as input HateMe C# 1 May 13th, 2004 11:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.