RE: [xsl] numbering question

Subject: RE: [xsl] numbering question
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 23 Dec 2008 11:49:10 -0000
Each 2nd-level <document> element is the first child of its parent, and
therefore gets the number 1.

Try count="/document | included | section"

(Or just generate the leading "1." by hand, as it's constant, and use
count="included | section").

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Trevor Nicholls [mailto:trevor@xxxxxxxxxxxxxxxxxx] 
> Sent: 23 December 2008 11:23
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] numbering question
> 
> I understand that the numbering reflects the input document. 
> There are multiple passes to the process and the document 
> which I enclose here (rather
> simplified) is an intermediate stage. It is a single 
> document; yes there are sourcefile paths stored in element 
> attributes but all the content is already localised in the 
> one input document as shown here.
> 
> Ignoring the before and after pieces of the application, and 
> just looking at the document as it stands, I cannot see why 
> the numbering is generating duplicates. In the version 
> included here I have modified the element titles to reflect 
> how I would have expected the numbering to work. You can see 
> the differences - hopefully the reason why my expectations 
> are wrong will be clear to somebody!
> 
> And as far as the purpose of these numbers is concerned, it 
> doesn't matter whether the numbers are unique within the 
> entire input document (as I would have expected), or only 
> unique within the subfile headings (as they _almost_ are). 
> What matters is that they are not duplicated at that subfile 
> level (and sadly, under the myself4 heading, the 1.1.1 numbers are).
> 
> Cheers
> Trevor
> 
> 
> XML
> ===
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <document>
> 	<title>Top level introduction - 1</title>
> 	<included srcfile="introduction.xml">
> 		<document>
> 			<title>Second level introduction - 1.1</title>
> 			<section>
> 				<title>Section introduction - 
> 1.1.1</title>
> 				<para>text</para>
> 			</section>
> 			<section>
> 				<title>About - 1.1.2</title>
> 				<para>text</para>
> 				<section>
> 					<title>New - 1.1.2.1</title>
> 					<para>text</para>
> 				</section>
> 				<section>
> 					<title>Old - 1.1.2.2</title>
> 					<para>text</para>
> 				</section>
> 			</section>
> 			<section>
> 				<title>Help - 1.1.3</title>
> 				<para>text</para>
> 			</section>
> 			<section>
> 				<title>Conventions - 1.1.4</title>
> 				<para>text</para>
> 			</section>
> 		</document>
> 	</included>
> 	<included srcfile="starting.xml">
> 		<document>
> 			<title>Starting - 1.2</title>
> 			<para>text</para>
> 			<section>
> 				<title>Before you start - 1.2.1</title>
> 				<para>text</para>
> 			</section>
> 			<section>
> 				<title>One way - 1.2.2</title>
> 				<para>text</para>
> 			</section>
> 			<section>
> 				<title>Another way - 1.2.3</title>
> 				<para>text</para>
> 			</section>
> 		</document>
> 	</included>
> 	<included secfile="basics.xml">
> 		<document>
> 			<title>Basics - 1.3</title>
> 			<included srcfile="basics_first.xml">
> 				<section>
> 					<title>Introduction - 
> 1.3.1</title>
> 					<para>text</para>
> 				</section>
> 			</included>
> 			<included secfile="basics_second.xml">
> 				<section>
> 					<title>Examples - 1.3.2</title>
> 					<para>text</para>
> 				</section>
> 			</included>
> 			<included srcfile="basics_third.xml">
> 				<section>
> 					<title>Resources - 1.3.3</title>
> 					<para>text</para>
> 				</section>
> 			</included>
> 		</document>
> 	</included>
> </document>
> 
> 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>&#xa;</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>&#xa;</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>
> 
> Output
> ======
> 
> myself1
>   1 - Top level introduction - 1
> myself2
>   1.1 - Second level introduction - 1.1
>   1.1.1 - Section introduction - 1.1.1
>   1.1.2 - About - 1.1.2
>   1.1.2.1 - New - 1.1.2.1
>   1.1.2.2 - Old - 1.1.2.2
>   1.1.3 - Help - 1.1.3
>   1.1.4 - Conventions - 1.1.4
> myself3
>   1.1 - Starting - 1.2
>   1.1.1 - Before you start - 1.2.1
>   1.1.2 - One way - 1.2.2
>   1.1.3 - Another way - 1.2.3
> myself4
>   1.1 - Basics - 1.3
>   1.1.1 - Introduction - 1.3.1
>   1.1.1 - Examples - 1.3.2
>   1.1.1 - Resources - 1.3.3
> 
> 
> 
> -----Original Message-----
> From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
> Sent: Tuesday, 23 December 2008 11:33 p.m.
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: [xsl] numbering question
> 
> 
> The xsl:number instruction generates a number that depends on 
> the position
> of the current node in the input document. Although you typically want
> numbers attached to nodes in the result tree, you are 
> actually computing
> numbers in relation to the source tree. If you have multiple 
> source trees,
> you can't get a single numbering sequence except by doing a two-phase
> transformation - first generate a tree with the structure of 
> the output,
> then transform it to add the numbers.
> 
> Michael Kay
> http://www.saxonica.com/

Current Thread