Solved

XSLT with nested Elements

Posted on 2011-03-09
1
400 Views
Last Modified: 2013-11-18



Hi,

I would like to number child Elements in the below XML using XSLT. How can we achieve this. basically there can be any number of child Elements nested.

Output :

1 child
1.1 child
1.1.1 child

1 child
1.1.

1 child
1.1 child










Input xml

<?xml version="1.0" encoding="utf-8"?>
<root>
  <child>
    <child>
      <child>
       
      </child>
     
    </child>    
 
  </child>

  <child>
    <child>

    </child>
  </child>

  <child>
    <child>
     
     
    </child>
   
  </child>
 
 
</root>



0
Comment
Question by:Techsavy
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 35093199

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:template match="root">
        <html>
            <body>
                <xsl:apply-templates select="child">
                    <xsl:with-param name="number" select="'1. '"/>
                </xsl:apply-templates>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="child">
        <xsl:param name="number"/>
        <p>
            <xsl:value-of select="$number"/>
            <xsl:value-of select="name()"/>
        </p>
        <xsl:apply-templates select="child">
            <xsl:with-param name="number" select="concat('1.', $number)"/>
        </xsl:apply-templates>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question