Re: [xsl] recursive template call, howto

Subject: Re: [xsl] recursive template call, howto
From: "Sam Carleton" <scarleton@xxxxxxxxxxxxxxxx>
Date: Tue, 22 Aug 2006 07:52:09 -0400
Charles,

Thank you, that gets me very close, except...

When I transformed the xml using the style sheet you provided, I got this:

   <table>
     <tr><td>1</td></tr>
     <tr><td>2</td></tr>
     <tr><td>7</td></tr>
     <tr><td>8</td></tr>
   </table>

   <table>
     <tr><td>3</td></tr>
     <tr><td>4</td></tr>
     <tr><td>5</td></tr>
     <tr><td>6</td></tr>
   </table>

The problem is that the two simple groups that came after the complex
group (lines 7 and 8) need to be in their own table after the complex
group, like this:

   <table>
     <tr><td>1</td></tr>
     <tr><td>2</td></tr>
   </table>

   <table>
     <tr><td>3</td></tr>
     <tr><td>4</td></tr>
     <tr><td>5</td></tr>
     <tr><td>6</td></tr>
   </table>

   <table>
     <tr><td>7</td></tr>
     <tr><td>8</td></tr>
   </table>

Sam

On 8/22/06, cknell@xxxxxxxxxx <cknell@xxxxxxxxxx> wrote:
Taking the liberty of assuming a document root named "root-element" (just substitute whatever is the case in your document), this will do what you asked. "Look Ma! No recursion!" (Pardon an American old enough to remember 1950's TV ads.)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

        <xsl:template match="/">
                <xsl:apply-templates />
        </xsl:template>

<xsl:template match="root-element">
    <html>
        <body>
                <table>
                        <xsl:apply-templates select="group[not(@type)]" />
                </table>
                <xsl:apply-templates select="group[@type]" />
        </body>
    </html>
  </xsl:template>

  <xsl:template match="group[not(@type)]">
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="group[@type]">
        <table>
        <xsl:apply-templates />
    </table>
  </xsl:template>

  <xsl:template match="line">
    <tr><td><xsl:value-of select="@id" /></td></tr>
  </xsl:template>

</xsl:stylesheet>
--
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Sam Carleton <scarleton@xxxxxxxxxxxxxxxx>
Sent:     Mon, 21 Aug 2006 22:13:53 -0400
To:       xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  [xsl] recursive template call, howto

Here is the data:

<group>
  <line id = "1"/>
</group>
<group>
  <line id="2"/>
</group>
<group type="complex">
  <line id="3"/>
  <line id="4"/>
  <line id="5"/>
  <line id="6"/>
</group>
<group>
  <line id="7"/>
</group>
<group>
  <line id="8"/>
</group>

The idea behind the data is that there are two types of groups: simple
and complex.  The simple do not have a "type" attribute (it could if it
would help things).  The end result should be HTML where the first two
simple groups are in one HTML table, than there is a second table for
each of the complex groups and thing a final table for the last three
simple groups.  The order of simple and complex groups is random.

My thought was when the first simple group was encountered, call a
recursive template starting at the simple group and building a nodeset
of lines until a complex group or the end was encountered.  I cannot
figure out how to do that in xsl.  Am I on the right path? If so, how
do I do it?  If not, what is the right path?

sam

Current Thread