This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

implement for loop with XSLT


I came up with the enclosed code to grab the value of an <n> element
and generate a list of integers and their squares between 1 and n:

   1 1
   2 4
   3 9
   ...
   8 64
   9 81
   10 100

I am a bit stunned at how intricate this code looks. Is there
a more direct way to accomplish this? It seems tempting to
use "for-each" as a built in iterator but you need a node-set
to walk. I don't have a node-set, I just have the number n.

If I am stuck with this recursive solution, is there a way
to write a generic "for" template but pass in a "body" that
needs to be evalutate? For example pass a string "$i*$i*$i"
to evaluate cubes).

  <xsl:with-param name="body" select="'$i*$i*$i'"/>
   
I can pass the above string but can't get it to evaluate within
the "for" template - it remains a string. 

Regards,

Dan

File: for.xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="for.xsl"?>
<root>
 <n>10</n>
</root>

File: for.xsl:
<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   
 <xsl:template match="/">
  <xsl:apply-templates select="root"/>
 </xsl:template>
 
 <xsl:template match="root">
  <xsl:variable name="i" select="1"/>
  <xsl:variable name="n" select="n"/>
  <xsl:call-template name="for">
   <xsl:with-param name="i" select="$i"/>
   <xsl:with-param name="n" select="$n"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="for">
  <xsl:param name="i"/>
  <xsl:param name="n"/>
  <xsl:value-of select="$i"/>
  <xsl:text> </xsl:text>
  <xsl:value-of select="$i*$i"/><br/>
  <xsl:if test="$i &lt; $n">
   <xsl:call-template name="for">
    <xsl:with-param name="i" select="$i+1"/>
    <xsl:with-param name="n" select="$n"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
 
</xsl:stylesheet>

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]