Re: [xsl] Looping a node in XSLT

Subject: Re: [xsl] Looping a node in XSLT
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Wed, 7 Mar 2007 18:54:44 +0530
Here is a XSLT 1.0 solution, tested with Xalan-J 2.7.0:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:param name="n" />

 <xsl:template match="node() | @*">
   <xsl:copy>
     <xsl:apply-templates select="node() | @*"/>
   </xsl:copy>
 </xsl:template>

 <xsl:template match="prod">
   <xsl:param name="x" select="0" />

   <xsl:if test="$x &lt; $n">
     <xsl:copy-of select="." />
     <xsl:apply-templates select=".">
       <xsl:with-param name="x" select="$x + 1" />
     </xsl:apply-templates>
   </xsl:if>
 </xsl:template>

</xsl:stylesheet>

When I invoke Xalan-J as following:

java org.apache.xalan.xslt.Process -in test.xml -xsl test.xsl -PARA
M n 4

I get output:

<?xml version="1.0" encoding="UTF-8"?>
<order>
 <orderid>10</orderid>
 <prod>
   <productid>100</productid>
   <productqty>200</productqty>
 </prod>
 <prod>
   <productid>100</productid>
   <productqty>200</productqty>
 </prod>
 <prod>
   <productid>100</productid>
   <productqty>200</productqty>
 </prod>
 <prod>
   <productid>100</productid>
   <productqty>200</productqty>
 </prod>
</order>

Hope this helps.

On 3/7/07, Senthilkumaravelan Krishnanatham <senthil@xxxxxxxxx> wrote:
Hi All,
I have requirement to loop through the node N number of times,
Is there any way I can accomplish in XSLT?


for example <order> <orderid>10</orderid> <prod> <productid>100</productid > <productqty>200< productqty> </prod> </order>

Desired out put
<order>
<orderid>10</orderid>
<prod>
<productid>100</productid >
<productqty>200< productqty>
</prod>
<prod>
<productid>100</productid >
<productqty>200< productqty>
</prod>
<prod>
<productid>100</productid >
<productqty>200< productqty>
</prod>
... N times
</order>

Thanks,
Senthil


--
Regards,
Mukul Gandhi

Current Thread