Subject: [xsl] numbering question From: "Trevor Nicholls" <trevor@xxxxxxxxxxxxxxxxxx> Date: Tue, 23 Dec 2008 23:25:16 +1300 |
Hello all I am getting results I don't understand when I try to number sections in my document. My input documents are created by amalgamating various subdocuments - this structure is reflected by the <included srcfile=""> elements in the sample document which follows. My output documents are also split into subfiles, and these splits are reflected in the output of the sample XSL stylesheet which also follows. Probably I misunderstand how xsl:number works, but there are two features of my sample output which surprise me; in one case it works to my advantage and in the other case it doesn't. A) Why does the numbering restart within each output subfile, i.e. why do the myself2, myself3 and myself4 files all give their first section the number "1.1"? (I'm actually quite pleased that they do this, because it's exactly the result I wanted, but I don't understand why.) B) Why are the generated numbers in the fourth output document not unique, i.e. why are the "Introduction" and "Examples" sections both numbered "1.1.1"? (In this case I don't want this happening, because duplicated section numbers are BAD and I get duplicate key errors further down the track.) Can somebody please explain to me why this numbering is happening? Thanks in advance, Trevor Sample XML: =========== <?xml version="1.0" encoding="UTF-8"?> <document> <title>Top level introduction</title> <included srcfile="introduction.xml"> <document> <title>Second level introduction</title> <section> <title>Section introduction</title> <para>text</para> </section> <section> <title>About</title> <para>text</para> <section> <title>New</title> <para>text</para> </section> <section> <title>Old</title> <para>text</para> </section> </section> <section> <title>Help</title> <para>text</para> </section> <section> <title>Conventions</title> <para>text</para> </section> </document> </included> <included srcfile="starting.xml"> <document> <title>Starting</title> <para>text</para> <section> <title>Before you start</title> <para>text</para> </section> <section> <title>One way</title> <para>text</para> </section> <section> <title>Another way</title> <para>text</para> </section> </document> </included> <included secfile="basics.xml"> <document> <title>Basics</title> <included srcfile="basics_first.xml"> <section> <title>Introduction</title> <para>text</para> </section> </included> <included secfile="basics_second.xml"> <section> <title>Examples</title> <para>text</para> </section> </included> <included srcfile="basics_third.xml"> <title>Resources</title> <para>text</para> </included> </document> </included> </document> Sample XSL: =========== <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"> <xsl:output method="text" /> <xsl:param name="myself" select="'myself'" /> <xsl:variable name="subfilecnt" select="count(//document)" /> <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template match="document"> <xsl:call-template name="subfilename" /> <xsl:text>
</xsl:text> <xsl:call-template name="num-gen" /> <xsl:apply-templates /> </xsl:template> <xsl:template match="section"> <xsl:call-template name="num-gen" /> <xsl:apply-templates /> </xsl:template> <xsl:template name="num-gen"> <xsl:text> </xsl:text> <xsl:number count="document | section" level="multiple" /> <xsl:value-of select="concat(' - ',title)" /> <xsl:text>
</xsl:text> </xsl:template> <xsl:template match="text()" /> <xsl:template name="containing-doc"> <xsl:apply-templates select="." mode="containing-doc" /> </xsl:template> <xsl:template match="*" mode="containing-doc"> <xsl:choose> <xsl:when test="self::document"> <xsl:for-each select="title"> <xsl:call-template name="subfilename" /> </xsl:for-each> </xsl:when> <xsl:otherwise> <xsl:apply-templates select=".." mode="containing-doc" /> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- return the output filename --> <xsl:template name="subfilename"> <xsl:value-of select="$myself" /> <xsl:call-template name="subfilenum" /> </xsl:template> <!-- return the output file number --> <xsl:template name="subfilenum"> <xsl:call-template name="padfilenum"> <xsl:with-param name="num"> <xsl:number count="document" level="any" /> </xsl:with-param> </xsl:call-template> </xsl:template> <!-- return num padded to the correct size --> <xsl:template name="padfilenum"> <xsl:param name="num" /> <xsl:choose> <xsl:when test="$subfilecnt > 999"><xsl:number value="$num" format="0001" /></xsl:when> <xsl:when test="$subfilecnt > 99"><xsl:number value="$num" format="001" /></xsl:when> <xsl:when test="$subfilecnt > 9"><xsl:number value="$num" format="01" /></xsl:when> <xsl:otherwise><xsl:number value="$num" format="1" /></xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> The output I get: ================= myself1 1 - Top level introduction myself2 1.1 - Second level introduction 1.1.1 - Section introduction 1.1.2 - About 1.1.2.1 - New 1.1.2.2 - Old 1.1.3 - Help 1.1.4 - Conventions myself3 1.1 - Starting 1.1.1 - Before you start 1.1.2 - One way 1.1.3 - Another way myself4 1.1 - Basics 1.1.1 - Introduction 1.1.1 - Examples
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Problem with grouping sib, Casper Voortman | Thread | RE: [xsl] numbering question, Michael Kay |
RE: [xsl] Using Java in XSLT to det, Hermann Stamm-Wilbra | Date | RE: [xsl] Sorting date format, Michael Kay |
Month |