[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: for-each loop question

Subject: Re: for-each loop question
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 3 Apr 2006 13:20:49 +0100
xml for loop
   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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.