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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Geert BormansInformation ArchitectCommented:
oooooh sorry, I have mixed up your actual output and desired output, give me a second
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Geert BormansInformation 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.