xsl-list
[Top] [All Lists]

RE: [xsl] for-each loop question

2006-04-03 05:43:07
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(_dot_)j(_dot_)welch(_at_)gmail(_dot_)com] 
Sent: 31 March 2006 12:21
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>