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 March 22nd, 2007, 08:26 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in recursive call

Hi,

 As i am newbie to this XSLT. Please help me, I have written code for converting XML to required XML structure.

My part of input file:

<ol type="1">
<li>First</li>
<li>Second</li>
</ol>

my XSL:

<xsl:template match="//ol">
<xsl:call-template name="list"/>
</xsl:template>


<xsl:template name="list">
        <list>
            <xsl:attribute name="listType"><xsl:text>ordered</xsl:text></xsl:attribute>
            <xsl:attribute name="styleCode">
            <xsl:choose>
            <xsl:when test="@type='1'">
            <xsl:text>Arabic</xsl:text>
            </xsl:when>
            <xsl:when test="@type='a'">
            <xsl:text>LittleAlpha</xsl:text>
            </xsl:when>
            <xsl:when test="@type='i'">
            <xsl:text>LittleRoman</xsl:text>
            </xsl:when>
            <xsl:when test="@type='I'">
            <xsl:text>BigRoman</xsl:text>
            </xsl:when>
            <xsl:when test="@type='A'">
            <xsl:text>BigAlpha</xsl:text>
            </xsl:when>
            <xsl:otherwise>
            <xsl:text>Arabic</xsl:text>
            </xsl:otherwise>
            </xsl:choose>
            </xsl:attribute>
            <xsl:apply-templates select="li"/>
        </list>
    </xsl:template>

But it is working fine for the above scenario. But its not working for list within list(nested list) like the following.


<ol type="1">
<li>First</li>
<li>Second</li>
<ol type="a">
<li>Inner1</li>
<li>Inner2</li>
<li>Inner3</li>
</ol>
<li>Third</li>
</ol></div>

The first level is only getting covnerted. My case will have n level nested like the above.

Thanks in advacne.



 
Old March 22nd, 2007, 09:45 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

For most XSLT transformations, and especially when handling an input structure that's recursive, you should use the standard recursive-descent design pattern where you write a template rule for matching each element, and it uses apply-templates to process its children.

<xsl:template match="//ol">
<xsl:call-template name="list"/>
</xsl:template>

The "//" at the start of the match pattern achieves nothing. Use match="ol". (The only difference is the priority of the rules if there are conflicts).

In your example the call-template achieves nothing, you could move the body of the called template inline.

<xsl:apply-templates select="li"/>

This says "process the li children". But your example also has "ol" children, and you aren't processing these. That's why they aren't being processed. Use <xsl:apply-templates/> to process all the children.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 22nd, 2007, 10:11 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mr Mkhay,

Thank you very much. It is working fine. Now i could make it what is the use of <xsl:applytemplates/>.



 
Old March 22nd, 2007, 11:18 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

> what is the use of <xsl:apply-templates/>

Apply-templates is central to the processing model of XSLT. Read about it in your favourite XSLT textbook, and come back with questions if there are specific points you don't understand.

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 when a code changed with call template rakesh XSLT 5 July 24th, 2008 11:16 AM
recursive function problem cnatale XSLT 2 April 3rd, 2008 03:11 AM
Problem in Recursive applying template ismomo XSLT 3 December 29th, 2007 11:35 PM
recursive query cathiec SQL Language 2 August 25th, 2006 05:58 AM
xsl:call-template name="$variable" problem chemi XSLT 1 October 6th, 2005 07:28 AM





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