Re: [xsl] problem with numbering

Subject: Re: [xsl] problem with numbering
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 16 Sep 2002 17:30:04 +0200
At 2002-09-16 16:15 +0200, Lars Geldner wrote:
I have a problem with the xsl:number-element. In the following part of a
XML-document I am not able to produce the numbering which I have declared in
the comments.

That is because your data is not structured the way you want it numbered, so you cannot use <xsl:number> ... this isn't a fault of <xsl:number>. <xsl:number> is used to reflect the structure of a document and your elements are not structured hierarchically as you want them numbered.


You will have to manually count the constructs given the structure that you have, as in the following working example:

T:\ftemp>type lars.xml lars.xsl

lars.xml


<NUMMERIERUNG> <!--1.--><NELEMENT>text1</NELEMENT> <!--2.--><NELEMENT>text2</NELEMENT> <NUMMERIERUNG> <!--2.1--><NELEMENT>text21</NELEMENT> <!--2.2--><NELEMENT>text22</NELEMENT> </NUMMERIERUNG> <!--3.--><NELEMENT>text3</NELEMENT> </NUMMERIERUNG>

lars.xsl


<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:template match="NELEMENT">
  <xsl:for-each select="ancestor::NUMMERIERUNG">
    <xsl:if test="position() > 1">
      <xsl:value-of select="count(preceding-sibling::NELEMENT)"/>
      <xsl:text>.</xsl:text>
    </xsl:if>
  </xsl:for-each>
  <xsl:value-of select="count(preceding-sibling::NELEMENT)+1"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt lars.xml lars.xsl
<?xml version="1.0" encoding="utf-8"?>

   1 text1
   2 text2

    2.1 text21
    2.2 text22

3 text3

T:\ftemp>


If you really wanted to use <xsl:number> then you will have to nest the structure of your input as follows:


T:\ftemp>type lars2.xml lars2.xsl

lars2.xml


<NUMMERIERUNG> <!--1.--><NELEMENT>text1</NELEMENT> <!--2.--><NELEMENT>text2 <NUMMERIERUNG> <!--2.1--><NELEMENT>text21</NELEMENT> <!--2.2--><NELEMENT>text22</NELEMENT> </NUMMERIERUNG> </NELEMENT> <!--3.--><NELEMENT>text3</NELEMENT> </NUMMERIERUNG>

lars2.xsl


<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:template match="NELEMENT">
  <xsl:number level="multiple"/>
  <xsl:text> </xsl:text>
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt lars2.xml lars2.xsl
<?xml version="1.0" encoding="utf-8"?>

   1 text1
   2 text2

    2.1 text21
    2.2 text22


3 text3


T:\ftemp>

Note how I placed the 2.1 and 2.2 *inside* the 2. NELEMENT so that the structure of the document reflects the numbering that you want ... *then* I can use <xsl:number> to get the numbering of the hierarchy.

I hope this helps.

........................ Ken

--
Upcoming hands-on in-depth    Europe:         Sep 18-Sep 20,2002
XSLT/XPath and XSL-FO         North America:  Sep 30-Oct  4,2002

G. Ken Holman               mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.        http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0  +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                     Definitive XSLT and XPath
ISBN 0-13-140374-5                             Definitive XSL-FO
ISBN 1-894049-08-X Practical Transformation Using XSLT and XPath
ISBN 1-894049-10-1             Practical Formatting Using XSL-FO
Next public training: 2002-09-18,19,30,10-03,12-08,2003-03-04,07


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



Current Thread