Solved

XSL Recursive Call - New to XSL

Posted on 2003-12-09
3
273 Views
Last Modified: 2013-11-19
I have an XML file :

<toc>
<section>
  <title>Chapter 1</title>

    <section>
      <title>1-1 Subtitle</title>
       
         <section>
            <title>1-1-1 Sub Sub Title</title>
         </section>

     </section>
   
</section>

<section>
  <title>Chapter 2</title>
</section>

.
.
.

</toc>

As you can see, I can have unlimited nested <section>  I am trying to write XSL the will process the tags like this:

toc -> section 1 -> sub section -> sub sub section
          section 2
         
At the moment my xsl looks like this:

<xsl:template match="//section">

        <xsl:element name="a">
                     <xsl:value-of select="title" />
         </xsl:element><br/>  
        <xsl:element name="div">
            <xsl:attribute name="id"><xsl:value-of select="@id" /></xsl:attribute>  
            <xsl:attribute name="style">display:none</xsl:attribute>
                       
                <xsl:apply-templates select="section" />
                       
        </xsl:element>                      
     

</xsl:template>

<xsl:template match="//section/section">
 
        <xsl:element name="div">
            <xsl:attribute name="id"><xsl:value-of select="@id" /></xsl:attribute>  
            <xsl:value-of select="title" />
                <xsl:apply-templates select="section" />
        </xsl:element>                      

</xsl:template>

For evey section to get processed I have to add another template matching "//section/section/section" and I have to keep doing that foe each nested section.  But There could be any number of nested sections.  Is there a generic way to process the elements that way?  The output is the sections will be in nested DIV tags on a html page
 
0
Comment
Question by:alzoid69
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
rdcpro earned 125 total points
ID: 9906310
Don't use wildcard XPath expressions.  This is what's causing your problems.  Just use a single template.  Recursion is built-in:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="section">
        <a>
            <xsl:value-of select="title"/>
        </a>
        <br/>
        <div id="{@id}">
            <xsl:apply-templates select="section"/>
        </div>
    </xsl:template>
</xsl:stylesheet>

which when applied to this XML:

<toc>
    <section id="1">
        <title>Chapter 1</title>
        <section id="1.1">
            <title>1-1 Subtitle</title>
            <section>
                <title>1-1-1 Sub Sub Title</title>
            </section>
        </section>
    </section>
    <section id="2">
        <title>Chapter 2</title>
    </section>
</toc>

produces this:

<?xml version="1.0" encoding="UTF-8"?>
<div>
    <a>Chapter 1</a>
    <br />
    <div id="1">
        <a>1-1 Subtitle</a>
        <br>
        <div id="1.1">
            <a>1-1-1 Sub Sub Title</a>
            <br>
            <div id="" />
        </div>
    </div>
    <a>Chapter 2</a>
    <br>
    <div id="2" />
</div>


Regards,
Mike Sharp
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 9906323
Disregard the outer <div> tag.  I put that in there manually so I could auto-indent the output.  The template above produces:

    <a>Chapter 1</a>
    <br />
    <div id="1">
        <a>1-1 Subtitle</a>
        <br>
        <div id="1.1">
            <a>1-1-1 Sub Sub Title</a>
            <br>
            <div id="" />
        </div>
    </div>
    <a>Chapter 2</a>
    <br>
    <div id="2" />

Regards,
Mike Sharp

0
 

Author Comment

by:alzoid69
ID: 9906376
Thanks Mike
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

758 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

20 Experts available now in Live!

Get 1:1 Help Now