We help IT Professionals succeed at work.
Get Started

Help with "xsl:number" for XSLT 2.0

388 Views
Last Modified: 2013-11-18
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

Comment
Watch Question
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE