Re: [xsl] Looping a node in XSLT

Subject: Re: [xsl] Looping a node in XSLT
From: Senthilkumaravelan Krishnanatham <senthil@xxxxxxxxx>
Date: Tue, 6 Mar 2007 16:57:00 -0800
I tried the same
<xsl:template match="products">
   <xsl:copy><xsl:template select="customer" /></xsl:copy>
</xsl:template>
<xsl:template match="customer">
   <xsl:copy-of select="for 1 to 10 return ." />
</xsl:template>

and getting the compile time error


Compiler warnings:
  line 5: Illegal attribute 'select'.
ERROR:  'Syntax error in 'for 1 to 10 return .'.'
FATAL ERROR:  'Could not compile stylesheet'

(Location of error unknown)XSLT Error (javax.xml.transform.TransformerConfigurationException): Could not compile stylesheet
Is there anyway I could use


On Mar 6, 2007, at 4:23 PM, Abel Braaksma wrote:

Senthilkumaravelan Krishnanatham wrote:
I have tried the same and hard time figuring out the syntax.

Considering your code piece below, you will never be able to run it, because it contains compile time errors. If you copied my example, you could've figured out the syntax:


1 to $n

instead of:

1to$n


Other things in your code that worry me a bit is why you use a call- template for something that only selects all the text nodes from the input, this can be refactored away. There is no matching template for "customer", is that on purpose? I can recommend you the XSLT books by Michael Kay and the beginners book by Jenni Tennison, they provide excellent coverage of some of the key concepts of XSLT (which is to many of us a not-so-trivial language to grasp when we first try it).


Something like this perhaps does what you want:

<xsl:template match="products">
   <xsl:copy><xsl:template select="customer" /></xsl:copy>
</xsl:template>
<xsl:template match="customer">
   <xsl:copy-of select="for 1 to 10 return ." />
</xsl:template>







<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/ XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" encoding="UTF-8" />
<xsl:template match="/products">
<xsl:copy>
<xsl:call-template name="COPY">
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="COPY">
<xsl:variable name="n" select="10" />
<xsl:for-each select="1to$n">
<xsl:copy>
<xsl:apply-templates select="//customer/*"/>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Current Thread