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

RE: numbering question

Subject: RE: numbering question
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 23 Dec 2008 10:33:27 -0000
RE:  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/  

> -----Original Message-----
> From: Trevor Nicholls [mailto:trevor@xxxxxxxxxxxxxxxxxx] 
> Sent: 23 December 2008 10:25
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject:  numbering question
> 
> 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>&#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>
> 
> 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

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.