Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xsl] for-each loop question

From: "Khorasani, Houman" <Houman.Khorasani@------------>
To:
Date: 4/3/2006 11:55:00 AM
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: [xsl] 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


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