Solved

XSLT with nested Elements

Posted on 2011-03-09
1
398 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
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 …

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now