Subject: infinite recursion
Posted By: bangbangbogi Post Date: 5/4/2006 5:19:38 AM
i am trying to extract information from another xml doc. what i have done is to use the document method. The output that results is an infinite recursion. The following is my code

<?xml version="1.0"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/TR/REC-html40">

<xsl:output method="html"/>

<xsl:variable name ="CSE4500xml" select="document('tutorialCSE4500.xml')"/>

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


<xsl:template match="unit">
    <HTML>
        <BODY>
        <H2>UNIT TIMETABLE:<xsl:value-of select="name"/></H2>
        Unit Code:
            <xsl:value-of select="code"/>
        <BR/>
        Unit Lecturer:
            <xsl:value-of select="unitLeader"/>
                <table border="1">
                    <th align="left">Tutorial ID</th>
                      <th align="left">Tutor</th>
                      <th align="left">TimeTable</th>
                    <xsl:apply-templates select="$CSE4500xml/tutorialClasses/unit"/>
                        <TD><xsl:value-of select="tutorialClasses/unit/class"/></TD>
                        <TD><xsl:value-of select="tutorialClasses/unit/tutor"/></TD>
                        <TD><xsl:value-of select="tutorialClasses/unit/timetable"/></TD>
                </table>

        </BODY>
    </HTML>   
</xsl:template>   

</xsl:stylesheet>
 I know that the error occurs in the line that I just put in bold. But i do not know how to resolve it. Please help.

Thanks

Reply By: joefawcett Reply Date: 5/4/2006 5:28:07 AM
You'll have to show some of the 'tutorialCSE4500.xml' for me to be sure but when you're inside the template that matches unit you make a call to $CSE4500xml/tutorialClasses/unit which then starts a recursion. As unit is the context node you can change the code to:

<table border="1"><tr>
                    <th align="left">Tutorial ID</th>
                      <th align="left">Tutor</th>
                      <th align="left">TimeTable</th></tr><tr>
                                            <TD><xsl:value-of select="class"/></TD>
                        <TD><xsl:value-of select="tutor"/></TD>
                        <TD><xsl:value-of select="timetable"/></TD></tr>
                </table>

That assumes you want a header for each unit which seems odd. You could move the header code a level up for example.

As I said if you this fails show an example of the source XML and your desired output.

On a side note why are you using the document function instead of just using the standard input method?

--

Joe (Microsoft MVP - XML)
Reply By: bangbangbogi Reply Date: 5/4/2006 5:50:14 AM
Hi joe, thanks for the quick reply. the expected output that i want is
UNIT TIMETABLE:

Unit Code:



Unit Lecturer:









Tutorial ID    Tutor    TimeTable            

the source file for the xml is

<?xml version="1.0"?>
<tutorialClasses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="tutorial.xsd">
    <unit>
        <unitCode>CSE4500</unitCode>
        <class ID="t1">
            <tutor>Evi Syukur</tutor>
            <timetable>
                <day>Wednesday</day>
                <time>2-4 PM</time>
            </timetable>
            <studentMark>
                <student ID="s112233441">
                    <name>James Hird</name>
                    <assignments>
                        <assignment no="1">80</assignment>
                        <assignment no="2">90</assignment>
                        <unitTest>75</unitTest>
                    </assignments>
                </student>



Reply By: mhkay Reply Date: 5/4/2006 5:54:11 AM
Within this template rule

<xsl:template match="unit">

you are doing

<xsl:apply-templates select="$CSE4500xml/tutorialClasses/unit"/>

which is going to match the template rule you are in, causing a recursive call. There's nothing to terminate the recursion so it goes on for ever.

Change the template to say match="units/unit": this will stop the recursive call. But I don't know what processing you actually want to apply to tutorialClasses/unit - perhaps you need another template rule to define this. Or perhaps you don't want to apply-templates to tutorialClasses/unit at all, since you then go on to output its details using xsl:value-of.

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

Go to topic 43785

Return to index page 296
Return to index page 295
Return to index page 294
Return to index page 293
Return to index page 292
Return to index page 291
Return to index page 290
Return to index page 289
Return to index page 288
Return to index page 287