[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: "Khorasani, Houman" <Houman.Khorasani@xxxxxxxxxxxx>
Date: Mon, 3 Apr 2006 12:54:27 +0100
xslt for each loop count
Hi Andrew et al,

I solved one issue like you said in a recursive way:

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)"/>
   <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" />
      <xsl:if test="$ProductCount &gt: 0">
        <xsl:for-each select="*/Product"
	    <xsl:call-template name="genericMapping">
	      <xsl:with-param name="quantity" select="$Quantity"/>
	    </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>
</xsl:template>


Description:
-------------
- The main Template shall calculate the amount of the products within
the input file and send it as a parameter to the ProductTemplate.

- ProductTemplate extracts the quantity attribute of the first product
and within the for-each loop it will send it as a parameter to the
GenericMapping template.

- The GenericMapping template does a loop according to its quantity for
the first product.


Problem:
---------
Coming back to ProductTemplate the for-each can not continue once the
GenericMapping template had been called.  So how can I point to the next
product in this case for calling the GenericMapping tamplate?


I hope you know what I mean ;)
Many thanks
Houman



-----Original Message-----
From: andrew welch [mailto:andrew.j.welch@xxxxxxxxx]
Sent: 31 March 2006 12:21
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re:  for-each loop question


No, as the context is a single <Product> node...

Doing what you are after is simple is XSLT 2.0, you could just use
select="1 to $quantity".

In 1.0 it's a lot harder, you will need to write a recursive named
template that calls itself decrementing the count with each call, or
use the Piez 'hack' of select="$someElementList[position() &lt;= 5]"

As you haven't said if you can use 2.0 or not, I won't expand on the
1.0 solutions until then :)

cheers
andrew

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.