Solved

XSL Recursive Call - New to XSL

Posted on 2003-12-09
3
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

734 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