troubleshooting Question

Help with "xsl:number" for XSLT 2.0

Avatar of numberkruncher
numberkruncherFlag for United Kingdom of Great Britain and Northern Ireland asked on
Web Languages and StandardsXML
7 Comments1 Solution389 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
Gertone (Geert Bormans)
Information Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2006

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros