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

LVL 13
numberkruncherAsked:
Who is Participating?
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
something came in between, so it took somewhat longer.

What you need to specify is the "from" attribute
    <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="any" format="1" from="document/section"/>
        </h3>
    </xsl:template>

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
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
0
 
numberkruncherAuthor Commented:
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)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Geert BormansInformation ArchitectCommented:
oooooh sorry, I have mixed up your actual output and desired output, give me a second
0
 
numberkruncherAuthor Commented:
> 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!
0
 
numberkruncherAuthor Commented:
Thanks again!
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
All Courses

From novice to tech pro — start learning today.