Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] for-each loop question

From: David Carlisle <davidc@--------->
To:
Date: 4/3/2006 12:37:00 PM
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
________________________________________________________________________


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent