Solved

XSL Recursive Call - New to XSL

Posted on 2003-12-09
3
274 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with SimpleXML charts with PHP 4 84
JQuery tracking event. 3 57
Problem to ToolkitScriptManager 2 52
XML XSL Message 3 14
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

24 Experts available now in Live!

Get 1:1 Help Now