xslt for-each nested loops.

Soldato
Joined
27 Mar 2003
Posts
2,710
Ok. I am probably missing something obvious here but here is my problem.

I am try to build a website using xslt transformation.

I have an xml document which has the following structure.

Code:
<ROOT>
	<ROOTITEM1/>
	<ROOTITEM1/>
	<ROOTITEM1/>
	<ROOTITEM1/>
	<ROOTITEM1/>
	<ROOTITEM1/>
	<ROOTITEM2>
		<CHILDITEM1/>
		<CHILDITEM1/>
	</ROOTITEM2>
	<ROOTITEM2/>
	<ROOTITEM2/>
	<ROOTITEM2/>
	<ROOTITEM3/>
		<CHILDITEM2/>
			<CHILDOFCHILDITEM1/>
		</CHILDITEM2>
	</ROOTITEM3>
	<ROOTITEM4>
		<CHILDITEM3/>
		<CHILDITEM3/>
		<CHILDITEM3/>
	</ROOTITEM4>
</ROOT>

now each of the elements have attributes and the structure I have employeed is to have ROOTITEM4 CHILD Element link all the other items together

ie.

Code:
<CHILDITEM3 ID="1" ROOTITEMID="1" ROOTITEM2ID="3" />


so I have an xlst file which is:

Code:
 <?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  >
	<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match ="/" xml:space="default">
	<h1>A header</h1>

	
	<table border="1px">
	 <tr>
	  <th>Item1</th>
	  <th>Item2</th>
	  <th>Item3</th>
	  <th>Item4</th>
	  <th>Item5</th>
	 </tr>
	 <xsl:for-each select="ROOT/ROOTITEM4/CHILDITEM3" xml:space="default" >
	 	<xsl:sort select="@CourseDateStart"/>
	 	<xsl:variable name="varItem1ID" select="@ChildITEM2ID"/>
	 	<tr>
	  	<td>
	   	
	   	<xsl:for-each select ="../ROOTITEM3" xml:space="default">
	    		<xsl:if test="$varItem1ID = CHILDITEM2/@ID" xml:space="default">
	     		<p>hello</p>
	    		</xsl:if>
	   	</xsl:for-each>

         		</td>
  	 		<td>
			<xsl:value-of select="substring(@Item1ID1,7,2)"/>
			<xsl:text>-</xsl:text>
			<xsl:value-of select="substring(@Item1ID,5,2)"/>
			<xsl:text>-</xsl:text>
			<xsl:value-of select="substring(@Item1ID,1,4)"/>
	      	  	<xsl:text> To </xsl:text>
			<xsl:value-of select="substring(@Item2ID,7,2)"/>
			<xsl:text>-</xsl:text>
			<xsl:value-of select="substring(@Item2ID,5,2)"/>
			<xsl:text>-</xsl:text>
			<xsl:value-of select="substring(@Item2ID,1,4)"/>
			 </td>
	 		<td>
	   		 test
	 		</td>
	 		<td>
	  		test
	 		</td>
	 		<td>
	  		 test
	 		</td>
        		</tr>
     		</xsl:for-each>
   </table>
	
		
		
		
		
	</xsl:template>
	
	

	

</xsl:stylesheet

so it loops through and fills in all the columns except the column that uses the second for-each loop.

what am I do wrong? Is it even possible to do what I want to do?
 
well the first for-each is looping through CHILDITEM3 elements
The inner for-each starts within the context of the CHILDITEM3 element its currently looped on and is attempting to go up a node level and attempting to get ROOTITEM3, but going up a level from CHILDITEM3 leaves you in ROOTITEM4 and in ROOTITEM4 there us no ROOTITEM3 element
 
well the first for-each is looping through CHILDITEM3 elements
The inner for-each starts within the context of the CHILDITEM3 element its currently looped on and is attempting to go up a node level and attempting to get ROOTITEM3, but going up a level from CHILDITEM3 leaves you in ROOTITEM4 and in ROOTITEM4 there us no ROOTITEM3 element

but using the ../ takes me back to the root element. I have used xml spy to make sure that the xpath is going to the right place but it still refuses to populate the information.

I think it is just not passing the variable I have created to this inner loop. Is there any way I can do this or is it a case I will have to restructure my xml.
 
well as I though it was something stupid.

Thanks GMan for the advice.

After I looked at the xpath released i was not hitting the root element as you suggested and added antoher ../ and it worked. so I now have ../../rootelement3.
 
Back
Top Bottom