Link to home
Start Free TrialLog in
Avatar of numberkruncher
numberkruncherFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help with "xsl:number" for XSLT 2.0

I am having some difficulties with the "xsl:number" element. I want my figures to be numbered with the following format:

Figure [Root-most section number]-[Number of figure in section]

In the snippet box below I have added a simple XSL along with an example input, the output that I am getting, and finally the output that I desire.

It would be greatly appreciated if somebody could tell me how to fix this!
XSL:
====
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">

    <xsl:strip-space elements="*"/>

    <xsl:template match="document">
        <html>
            <head>
                <title>Test</title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    
    <xsl:template match="section">
        <h1>
            <xsl:text>Section </xsl:text>
            <xsl:number count="section" level="multiple" format="1.1"/>
        </h1>
        <div>
            <xsl:apply-templates/>
        </div>
    </xsl:template>

    <xsl:template match="figure">
        <h3>
            <xsl:text>Figure </xsl:text>
            <xsl:number count="document/section | figure" level="multiple" format="1-1"/>
        </h3>
    </xsl:template>

</xsl:stylesheet>


INPUT XML:
==========
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<document>
    <section>
        <section>
            <figure>A</figure>
            <figure>B</figure>
        </section>
        <section>
            <figure>C</figure>
            <figure>D</figure>
        </section>
    </section>
    <section>
        <section>
            <figure>E</figure>
            <figure>F</figure>
        </section>
        <section>
            <figure>G</figure>
            <figure>H</figure>
        </section>
    </section>
</document>


EXPECTED OUTPUT:
================
<html> 
   <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      <title>Test</title> 
   </head> 
   <body> 
      <h1>Section 1</h1> 
      <div> 
         <h1>Section 1.1</h1> 
         <div> 
            <h3>Figure 1-1</h3> 
            <h3>Figure 1-2</h3> 
         </div> 
         <h1>Section 1.2</h1> 
         <div> 
            <h3>Figure 1-3</h3> <!-- <<< LOOK HERE -->
            <h3>Figure 1-4</h3> 
         </div> 
      </div> 
      <h1>Section 2</h1> 
      <div> 
         <h1>Section 2.1</h1> 
         <div> 
            <h3>Figure 2-1</h3> 
            <h3>Figure 2-2</h3> 
         </div> 
         <h1>Section 2.2</h1> 
         <div> 
            <h3>Figure 2-3</h3> <!-- <<< LOOK HERE -->
            <h3>Figure 2-4</h3> 
         </div> 
      </div> 
   </body> 
</html>


ACTUAL OUTPUT:
==============
<html> 
   <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      <title>Test</title> 
   </head> 
   <body> 
      <h1>Section 1</h1> 
      <div> 
         <h1>Section 1.1</h1> 
         <div> 
            <h3>Figure 1-1</h3> 
            <h3>Figure 1-2</h3> 
         </div> 
         <h1>Section 1.2</h1> 
         <div> 
            <h3>Figure 1-1</h3> <!-- <<< LOOK HERE -->
            <h3>Figure 1-2</h3> 
         </div> 
      </div> 
      <h1>Section 2</h1> 
      <div> 
         <h1>Section 2.1</h1> 
         <div> 
            <h3>Figure 2-1</h3> 
            <h3>Figure 2-2</h3> 
         </div> 
         <h1>Section 2.2</h1> 
         <div> 
            <h3>Figure 2-1</h3> <!-- <<< LOOK HERE -->
            <h3>Figure 2-2</h3> 
         </div> 
      </div> 
   </body> 
</html>

Open in new window

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

try this

    <xsl:template match="figure">
        <h3>
            <xsl:text>Figure </xsl:text>
            <xsl:number count="document/section" level="multiple" format="1"/>
            <xsl:text>-</xsl:text>
            <xsl:number count="figure" level="single" format="1"/>
        </h3>
    </xsl:template>

Note that you will have duplicate numbers, but from your required result you seem OK with that
Avatar of numberkruncher

ASKER

Hi Gertone,

That seems to work in exactly the same way as what I had before. Just to clarify, here is the output that I would like:

Section 1
---Section 1.1
------Figure 1-1
------Figure 1-2
---Section 1.2
------Figure 1-3
------Figure 1-4
Section 2
---Section 2.1
------Figure 2-1
------Figure 2-2
---Section 2.2
------Figure 2-3
------Figure 2-4

(dashes just for spacing)
oooooh sorry, I have mixed up your actual output and desired output, give me a second
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> something came in between, so it took somewhat longer.

No problem pal. Thanks for your help, as always it is greatly appreciated!

> What you need to specify is the "from" attribute

I wasn't aware of that attribute, that works perfectly!
Thanks again!