Solved

XSLT with nested Elements

Posted on 2011-03-09
1
391 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
Comment Utility

<?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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

763 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

15 Experts available now in Live!

Get 1:1 Help Now