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 December 20th, 2007, 11:40 PM
Registered User
 
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in Recursive applying template

Hi all,
   I am quite new with xslt, I need advice on how to implement this.
   My XML structure:
Code:
        <sub_hierarchy lom_id="341584">
−
    <level id="22">
<title padding="15" type="link" updated_date="2005-05-30 10:43:51" dispatcher_name="Lim Wee Shing Tonny" id="10" have_child="2">lesson 2</title>
−
    <level id="10">
<title padding="30" type="link" updated_date="2005-05-30 10:43:51" dispatcher_name="Lim Wee Shing Tonny" id="90" have_child="1">lesson 3</title>
</level>
</level>
−
    <level id="69">
<title padding="15" type="link" updated_date="2005-05-30 10:43:51" dispatcher_name="Lim Wee Shing Tonny" id="52" have_child="1">testing 2</title>
</level>
</sub_hierarchy>


  Here is my XSLT:
Code:
<body>
        <xsl:apply-templates select="level" />
</body>
</xsl:template>
<xsl:template match="level">
    <xsl:for-each select='.'>
    <ul id="ul_menu">
        <xsl:choose>
            <xsl:when test="title">
                <xsl:apply-templates select="title" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates name="level" />
            </xsl:otherwise>
        </xsl:choose>
    </ul>
    </xsl:for-each>
</xsl:template>
<xsl:template match="title">
    <xsl:choose>
        <xsl:when test="@have_child &gt; 1">
            <li class="parent">
                <a href="javascript:;" onClick="toggle({@id})" onMouseOver="getInfo(this);" style="padding-left:{@padding}px;
">
                    <img src="/Elearning_Platform/images/plus.gif" />&nbsp;
                    <xsl:value-of select="." />
                </a>
                <input type="hidden" name="lessonplan_Dispatcher" value="{@dispatcher_name}" />
                <input type="hidden" name="lessonplan_Date" value="{@updated_date}" />
            </li>
        </xsl:when>
        <xsl:otherwise>
            <li class="unit">
                <a href="javascript:;" onMouseOver="getInfo(this);" style="padding-left:{@padding}px;">
                    <xsl:value-of select="." />
                </a>
                <input type="hidden" name="lessonplan_Dispatcher" value="{@dispatcher_name}" />
                <input type="hidden" name="lessonplan_Date" value="{@updated_date}" />
            </li>
 </li>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>


But, it doesn't work as expected. Can anyone tell me to do the right things ? Thanks. The result I would like to have is a tree structure, when found attribute @have_child > 1, it will has a child underneath it.
Lesson 2
   Lesson 3
Testing


Thanks.

 
Old December 21st, 2007, 05:28 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Hi

I'm not quite sure what you actual problem is, but here's some pointers about things I've spotted in your XSLT which probably don't work. From there hopefully you can get closer to what you are trying to do.

First, when you call <xsl:apply-templates select="level"/> this will apply the found template once for each level element - it does not pass an 'array' of level elements to that one template.

This means that the <xsl:for-each select="."> statement in your level template is doing nothing - it mealy loops once for the level.

Perhaps what you really wanted to do was select every child of the level element, in which case <xsl:for-each select="*"> is what you are after ("*" is equivalent to "child::*", "." is equivalent to "self::*"). Then your <xsl:if> statement could be changed to <xsl:if test="self::title">.

However as you don't actually do anything with the title or level elements inside the if you might as well get rid of the if completely and just do <xsl:apply-template select="."/>

<xsl:template match="level">
    <xsl:for-each select="child::*">
    <ul id="ul_menu">
         <xsl:apply-templates select="." />
    </ul>
    </xsl:for-each>
</xsl:template>


/- Sam Judson : Wrox Technical Editor -/
 
Old December 21st, 2007, 06:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, <xsl:for-each select="."> is a null operation. You can simply remove it - but try to understand why, because the fact that you wrote it suggests you have some misunderstanding about the way templates work.

In this code

<xsl:choose>
            <xsl:when test="title">
                <xsl:apply-templates select="title" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates name="level" />
            </xsl:otherwise>
        </xsl:choose>

I assume the name="level" should be select="level" (it won't even compile as written). With this change, it's saying "if there is a title child, process the title child, otherwise process the level child". Well there's nothing wrong with that, but I suspect that when an element has a title child and a level child then you actually want to process both. Which you can do simply by calling <xsl:apply-templates/> - no xsl:choose needed, no select attribute needed.

When your input contains a level as a direct child of another level, the simple approach will output a ul as a direct child of a ul, which isn't correct HTML - there needs to be an intervening li. One way to achieve this (in XSLT 2.0) is to have a template

<xsl:template match="level/level">
  <li>
    <xsl:next-match/>
  </li>
</xsl:template>

which generates the li element and then implicitly invokes the match="level" template. The next-match instruction isn't available in 1.0; you could achieve it by giving the match="level" template a name and invoking it using call-template.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 29th, 2007, 11:35 PM
Registered User
 
Join Date: Dec 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
  Thanks for replying and all the advices been provided. Ya, I realize I did make a mistaken in <xsl:foreach select=".">. Hence, I have make some changes on the code by

Code:
<xsl:template match="sub_hierarchy">
        <ul id="ul_menu"><xsl:apply-templates select="level" /></ul>
</xsl:template>
<xsl:template match="level">
            <xsl:when test="title">
                <xsl:apply-templates select="title" />
            </xsl:when>
            <xsl:otherwise-->
                <xsl:apply-templates />
        </xsl:choose-->
</xsl:template>
<xsl:template match="title">
    <xsl:choose>
        <xsl:when test="@have_child &gt; 1">
        <ul id="ul_menu" style="display:{@display}">
            <li class="parent" id="{../../@lom_id}_{@id}">
                <!--a href="javascript:;" onClick="javascript:alert('why');" onMouseOver="getInfo(this);" style="padding-left
:{@padding}px;"-->
                <a href="javascript:;" onClick="toggle('{../../@lom_id}_{@id}', {@have_child});" style="padding-left:{@paddin
g}px;">

                    <img src="/Elearning_Platform/images/plus.gif" />&nbsp;
                    <xsl:value-of select="." />
                </a>
                <input type="hidden" name="lessonplan_Dispatcher" value="{@dispatcher_name}" />
                <input type="hidden" name="lessonplan_Date" value="{@updated_date}" />
            </li>
        </ul>
        </xsl:when>
     <xsl:otherwise>

            <li class="unit" id="{../@id}" style="display:{@display}">
                <a href="javascript:;" onMouseOver="getInfo(this);" style="padding-left:{@padding}px;">
                    <xsl:value-of select="." />
                </a>
                <input type="hidden" name="lessonplan_Dispatcher" value="{@dispatcher_name}" />
                <input type="hidden" name="lessonplan_Date" value="{@updated_date}" />
                <xsl:choose>
                    <xsl:when test="@type = 0">
                        <input type="hidden" name="lessonplan_Type" value="--" />
                    </xsl:when>
                    <xsl:otherwise>
                        <input type="hidden" name="lessonplan_Type" value="{@type}" />
                    </xsl:otherwise>
                </xsl:choose>
            </li>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>
The xml structure will be same. But, I have found a problem in grouping all the same parent root into ONE UL. Here, I would like to explain further by taking the example of my previous xml structure,

I would like the structure to be [list]
   <li>Lesson 1</li>
   [list]
      <li>Lesson 1 child 0</li>
      <li>Lesson 1 child 1</li>
   </ul>
   <li>Lesson 2</li>
</ul>

But, my current xslt fail to tie the relationship of parent and child together when it is more than 2 level. Thanks for advice.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive Template Help bucky483 XSLT 1 July 20th, 2007 12:21 PM
Recursive template - Please Help! Tre XSLT 5 March 20th, 2007 11:23 AM
*solved* applying same template to multiple nodes Brian Campbell XSLT 3 October 13th, 2006 06:56 PM
Applying a template with subreport nat246 Crystal Reports 0 December 12th, 2005 02:50 AM
Using a recursive template for various nodes spencer.clark XSLT 5 August 2nd, 2005 03:49 PM





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