Link to home
Start Free TrialLog in
Avatar of alzoid69
alzoid69

asked on

XSL Recursive Call - New to XSL

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
 
ASKER CERTIFIED SOLUTION
Avatar of rdcpro
rdcpro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of alzoid69
alzoid69

ASKER

Thanks Mike