Re: [xsl] for-each loop question

Subject: Re: [xsl] for-each loop question
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 3 Apr 2006 13:20:49 +0100
   Input XML:
   ----------
   <RetainedProduct>
		   <Product Quantity="2" Code="A56390"/>
		   <Product Quantity="3" Code="A70978"/>
   </RetainedProduct>


   Main template:
   ------------------
   <xsl:template name="mainTemplate">

      <xsl:variable name="ProductCount"
   select="count(RetainedProduct/Product)"/>
You probably don't need this variable.

      <xsl:call-template name="productTemplate ">
	<xsl:with-param name="ProductCount" select="$ProductCount"/>
   </xsl:call-template>



   ProductTemplate:
   ------------------
   <xsl:template name="productTemplate">
	   <xsl:param name="ProductCount"/>
	   <xsl:variable name="Quantity" select="*/Product/@Quantity" />
This variable selects a node set of all the Quantity attributes of all
the Products.

	 <xsl:if test="$ProductCount &gt: 0">
 This test does nothing as if it is false the following for-each would
 produce no output anyway so there is no need to skip it.

	   <xsl:for-each select="*/Product"
	       <xsl:call-template name="genericMapping">
		 <xsl:with-param name="quantity" select="$Quantity"/>

This means that you pass the same $quantity value (the set of all
quantity attributes)  for each product. I think you just want to pass in
the current Quantity attribute ie select="@Quantity, which moeans that
you do notneed the Quantity variable defined above.

	       </xsl:call-template>
	     </xsl:for-each> 
	   </xsl:if>
   </xsl:template>


   GenericMapping template (Recursive):
   -------------------
   <xsl:template name="genericMapping">
	  <xsl:param name="quantity"/>
	  ...

	  <xsl:call-template name="genericMapping">
	     <xsl:with-param name="quantity" select="$quantity - 1"/>
	    </xsl:call-template>
This recursive call needs to be guarded by an
xsl:if test="$quantity  &gt;1"
otherwise you will recurse forever.

   </xsl:template>

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread