Solved

XSL Recursive Call - New to XSL

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

696 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