Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2007, 10:15 AM
Registered User
 
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Recursive Template Help

Hello all,

I am trying to transform the following XML into the HTML format below. The basic flow of it should go as follows, I believe.

1.If node contains children call template-GetAllChildren.
2.Get each child that doesn't have a child of its own.
3.If you come across a child that contains it's own child then end the current table </table> and start a new <fieldset> with <legend>.

XML:
Code:
<Person>
    <ID Number>777-77-7777</ID Number>
    <Name>
        <FirstName>Matt</FirstName>
        <LastName>Doe</LastName>
    </Name>
    <Address>XXXX</Address>
    <City>Miami</City>
    <State>FL</State>
    <Zip>99999</Zip>
</Person>
Desired HTML Format:
Code:
<html>
    <body>
        <fieldset>
            <legend>Person</legend>
            <table>
                <tr>
                    <td>ID Number</td>
                    <td>777-77-7777</td>
                </tr>
            </table>
                <fieldset>
                    <legend>Name</legend>
                    <table>
                        <tr>
                            <td>FirstName</td>
                            <td>Matt</td>
                        </tr>
                        <tr>
                            <td>LastName</td>
                            <td>Doe</td>
                        </tr>
                    </table>
                </fieldset>            
            <table>
                <tr>
                    <td>Address</td>
                    <td>XXXX</td>
                </tr>
                <tr>
                    <td>City</td>
                    <td>Miami</td>
                </tr>
                <tr>
                    <td>State</td>
                    <td>FL</td>
                </tr>
                <tr>
                    <td>Zip</td>
                    <td>99999</td>
                </tr>
            </table>
        </fieldset>
    </body>
</html>

XSL Template: (not working correctly)
Code:
<xsl:template name="FindAllChildren">

    <xsl:for-each select="(child::*)">

        <xsl:if test="not(child::*)">
            <tr>
                <td>
                    <xsl:value-of select="name()"/>        
                </td>
            </tr>
        </xsl:if>

        <xsl:if test="(child::*)">
            <tr>
                <td>
                    <fieldset>
                        <legend>
                            <xsl:value-of select="name()"/>
                        </legend>
                        <table>
                            <xsl:call-template name="FindAllChildren">
                            </xsl:call-template>    
                        </table>
                    </fieldset>
                </td>
            </tr>
        </xsl:if>

    </xsl:for-each>            
</xsl:template>
Thanks,

Matt

 
Old July 20th, 2007, 12:21 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, you need to learn how to use match templates and xsl:apply-templates. By ignoring these facilities you are working with one hand tied behind your back.

Secondly, you need to understand that you are writing nodes to a tree, not tags to a text file. This kind of thing doesn't make sense: "then end the current table </table> and start a new <fieldset> ". That's not the kind of operation XSLT does. Instructions in XSLT write nodes, and you can't write half a node.

What you seem to be doing here is adjacency grouping - group a maximal sequence of elements having some common property into a wrapper element. The property in question is having (or not-having) a child element. That's easily done in XSLT 2.0 with group-adjacent:

<xsl:for-each-group select="*" group-adjacent="exists(*)">
  <xsl:choose>
     <xsl:when test="current-grouping-key()">
        <table>
          <xsl:apply-templates/>
        </table>
     </xsl:when>
     <xsl:otherwise>
        <fieldset>
           <xsl:apply-templates/>

Doing the same thing in 1.0 is much more difficult.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in Recursive applying template ismomo XSLT 3 December 29th, 2007 11:35 PM
Recursive template - Please Help! Tre XSLT 5 March 20th, 2007 11:23 AM
transform in a loop vs recursive template ramarc XSLT 3 April 10th, 2006 04:40 PM
Using a recursive template for various nodes spencer.clark XSLT 5 August 2nd, 2005 03:49 PM
Recursive template to split up string and extract alexshiell XSLT 2 December 16th, 2004 09:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.