Link to home
Create AccountLog in
Avatar of bobbyrellis
bobbyrellis

asked on

Having trouble using nested for each in XSLT

I'm writing an application to assit creating weekly summaries for my develeopment activities for me to provide to my development team.  I'm learning XSLT/XML to do this.  I've created an XML file that contains multiple <summary> elements.  These elements contain details about the daily activities.  They also contain a list of <task> elements to outline development tasks.

I've created an XSLT file that should iterate through the summary elements and then itereate through the tasks for each day.  The script that I have will iterate through the summaries, however, the output doesn't show any of the tasks.  Any help?

Thanks.
<?xml version="1.0" encoding="utf-8"?>
<summarylist>
	<summary>
	  <day>Tuesday</day>
	  <date>02/03/2009</date>
	  
	  <workstarttime>8:00AM</workstarttime>
	  <workendtime>8:00AM</workendtime>
	  
	  <worklocation>Bryan's House</worklocation>
	  
	  <tasks>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  </tasks> 
	</summary>
	<summary>
	  <day>Monday</day>
	  <date>02/02/2009</date>
	  
	  <workstarttime>8:00AM</workstarttime>
	  <workendtime>8:00AM</workendtime>
	  
	  <worklocation>Bryan's House</worklocation>
	  
	  <tasks>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  	<task>
	  		<taskstarttime>8:00AM</taskstarttime>
	  		<taskendtime>10:25AM</taskendtime>
	  		<totaltimespent>2 hours 25 minutes</totaltimespent>
	  		
	  		<taskdescription>Worked on DEV-04-4939 Extending Download button</taskdescription>
	  		
	  		<tasknotes>Implemented new web form.  Tried this. Did that</tasknotes>
	  		
	  		<originalestimate>20 hours</originalestimate>
	  		
	  		<currenttimeestimate>17 hours 35 minutes</currenttimeestimate>
			  		
	  		<status> </status>
	  	</task>
	  </tasks> 
	</summary>
</summarylist>
 
==========================================================
 
 
 
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
  <html>
  <body>
    <h2>IBB Solutions weekly summary</h2>
    <xsl:for-each select="summarylist/summary">
    	<div>
	    	Summary for <b><xsl:value-of select="day"/><br/></b>
	    	<br/>Start Time: <b><xsl:value-of select="workstarttime"/></b>
	    	<br/>End  Time: <b><xsl:value-of select="workendtime"/></b>
	    	<br/>Work location: <b><xsl:value-of select="worklocation"/></b>
	    	<table border="1">
		    <tr bgcolor="#9acd32">
		      <th align="left">Task Description</th>
		      <th align="left">start Time</th>
		      <th align="left">End Time</th>
		      <th align="left">Original Estimate</th>
		      <th align="left">Time Spent</th>
		      <th align="left">Current Estimate</th>
		      <th align="left">Status</th>
		    </tr>
		    <xsl:for-each select="summarylist/summary/tasks/task">
		    <tr>
		      <td><xsl:value-of select="summarylist/summary/tasks/task/taskdescription"/></td>
		      <td><xsl:value-of select="summarylist/summary/tasks/task/taskstarttime"/></td>
		      <td><xsl:value-of select="summarylist/summary/tasks/task/taskendtime"/></td>
		      <td><xsl:value-of select="summarylist/summary/tasks/task/originalestimate"/></td>
		      <td><xsl:value-of select="summarylist/summary/tasks/task/totaltimespent"/></td>
		      <td><xsl:value-of select="summarylist/summary/tasks/task/currenttimeestimate"/></td>
		      <td><xsl:value-of select="summarylist/summary/tasks/task/status"/></td>
		    </tr>
		    </xsl:for-each>
		    
 
		    </table>
	    </div>
    </xsl:for-each>
  </body>
  </html>
</xsl:template></xsl:stylesheet>

Open in new window

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

you change the context node with for-each,
that means that inside the for loop,
the current context is the node which you are currently iterating on (if this sentence makes sense at all :-)
in short
                            <xsl:for-each select="summarylist/summary/tasks/task">
should be
                            <xsl:for-each select="tasks/task">
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of bobbyrellis
bobbyrellis

ASKER

Thanks a lot.. worked great.
you are welcome

since you are learning, I would recommend that you learn to adapt a different programming style.
In XSLT programming it is a good style to avoid xsl:for-each for pulling in nodes if you don't have to. The hierarchical structure of XML will force you into complex nested constructs.
It is a lot easier top use a style where you pushed out the child nodes for evaluation by a set of templates.
Instead of having all the code nested, you will end up with a set of "rules" in seperate templates.
That is a lot easier to maintain.
I have changed your stylesheet accordingly. Please learn to use that style. You will soon be very happy that you did
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
        <html>
            <body>
                <h2>IBB Solutions weekly summary</h2>
                <xsl:apply-templates select="summarylist/summary"/>
             </body>
        </html>
    </xsl:template>
    
    <xsl:template match="summarylist/summary">
        <div>
            Summary for <b><xsl:value-of select="day"/><br/></b>
            <br/>Start Time: <b><xsl:value-of select="workstarttime"/></b>
            <br/>End  Time: <b><xsl:value-of select="workendtime"/></b>
            <br/>Work location: <b><xsl:value-of select="worklocation"/></b>
            <table border="1">
                <tr bgcolor="#9acd32">
                    <th align="left">Task Description</th>
                    <th align="left">start Time</th>
                    <th align="left">End Time</th>
                    <th align="left">Original Estimate</th>
                    <th align="left">Time Spent</th>
                    <th align="left">Current Estimate</th>
                    <th align="left">Status</th>
                </tr>
                <xsl:apply-templates select="tasks/task"/>
            </table>
        </div>
    </xsl:template>
  
    <xsl:template match="tasks/task">
        <tr>
            <td><xsl:value-of select="taskdescription"/></td>
            <td><xsl:value-of select="taskstarttime"/></td>
            <td><xsl:value-of select="taskendtime"/></td>
            <td><xsl:value-of select="originalestimate"/></td>
            <td><xsl:value-of select="totaltimespent"/></td>
            <td><xsl:value-of select="currenttimeestimate"/></td>
            <td><xsl:value-of select="status"/></td>
        </tr>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window