[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Plse Help! something to do with counter....


Subject: Re: [xsl] Plse Help! something to do with counter....
From: "Poh Justin KT" <nitsujpoh@xxxxxxxxxxx>
Date: Wed, 02 May 2001 15:51:25 +0800

Thanks for your reply. I tried to change the codes to fit into my XSL but failed. This is my XSL..

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><body>
<form method="GET" action="../servlet/takeLoan">
<xsl:for-each select="List/Resource">
<div align="right">
<xsl:call-template name="checkboxes">
<xsl:with-param name="max" select="'5'" />
</xsl:call-template>
</div><br/>
</xsl:for-each>
</form>
</body></html>
</xsl:template>

<xsl:template name="checkboxes">
  <xsl:param name="count" select="0" />
  <xsl:param name="max" select="1" />
  <xsl:if test="$count &lt; $max">
  <input type="checkbox" name="{Index}" value="c{$count}" />
  <xsl:call-template name="checkboxes">
        <xsl:with-param name="count" select="$count + 1" />
        <xsl:with-param name="max" select="$max" />
  </xsl:call-template>
  </xsl:if>
</xsl:template>
</xsl:stylesheet>

and my resulting output in html look like this:
<html>
<body>
<form action="../servlet/takeLoan" method="GET">
<div align="right">
<input value="c0" name="1" type="checkbox">
<input value="c1" name="1" type="checkbox">
<input value="c2" name="1" type="checkbox">
<input value="c3" name="1" type="checkbox">
<input value="c4" name="1" type="checkbox">
</div>
<br>
<div align="right">
<input value="c0" name="6" type="checkbox">
<input value="c1" name="6" type="checkbox">
<input value="c2" name="6" type="checkbox">
<input value="c3" name="6" type="checkbox">
<input value="c4" name="6" type="checkbox">
</div>
<br>
<div align="right">
<input value="c0" name="8" type="checkbox">
<input value="c1" name="8" type="checkbox">
<input value="c2" name="8" type="checkbox">
<input value="c3" name="8" type="checkbox">
<input value="c4" name="8" type="checkbox">
</div>
<br>
<div align="right">
<input value="c0" name="9" type="checkbox">
<input value="c1" name="9" type="checkbox">
<input value="c2" name="9" type="checkbox">
<input value="c3" name="9" type="checkbox">
<input value="c4" name="9" type="checkbox">
</div>
<br>
<div align="right">
<input value="c0" name="10" type="checkbox">
<input value="c1" name="10" type="checkbox">
<input value="c2" name="10" type="checkbox">
<input value="c3" name="10" type="checkbox">
<input value="c4" name="10" type="checkbox">
</div>
<br>
</form>
</body>
</html>

there will be 5 sets of c0-c4.. what should i do to make it to be like this?(taking max as a constant with a value of 5)
<html>
<body>
<form action="../servlet/takeLoan" method="GET">
<div align="right">
<input value="c0" name="1" type="checkbox">
</div><br><div align="right">
<input value="c1" name="6" type="checkbox">
</div><br><div align="right">
<input value="c2" name="8" type="checkbox">
</div><br><div align="right">
<input value="c3" name="9" type="checkbox">
</div><br><div align="right">
<input value="c4" name="10" type="checkbox">
</div><br></form>
</body>
</html>


Thanks for all the help!

From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Reply-To: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
To: "Poh Justin KT" <nitsujpoh@xxxxxxxxxxx>
CC: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Plse Help!  something to do with counter....
Date: Mon, 30 Apr 2001 09:21:51 +0100

Hi Justin,

> Hi, I'm trying to create a form with checkboxes using XSLT, with
> each checkbox a different name, like "c1", "c2" util the last. a
> checkbox will be assign to each item in the XML.
>
> Is there a way to make a auto-increment counter and use it in the
> following tag?
> <input type="checkbox" name="XXX" value="{Index}">

The easiest thing is if you have nodes in your source XML that
represent the checkboxes that you want to create.  If you do, then you
can apply templates to those nodes, and use their position() to
generate the number, or even use xsl:number if you feel like it.  So
for example, if each of the checkboxes were represented by a checkbox
element in your source XML, you could apply templates to them:

<xsl:apply-templates select="checkbox" />

Then have a template that creates the input element when it's applied
to a checkbox element:

<xsl:template match="checkbox">
   <input type="checkbox" name="XXX" value="c{position()}" />
</xsl:template>

If you only have the number of checkboxes you want, and not nodes to
represent them, then you can use either a recursive template or the
Piez Method for iterating a set number of times.

Using recursion, you have a template that takes two parameters: a
count and a maximum.  Within the template, you emit the input element
you want, based on the $count parameter, and then, if the count is
less than the maximum, call the template again:

<xsl:template name="checkboxes">
   <xsl:param name="count" select="1" />
   <xsl:param name="max" select="1" />
   <input type="checkbox" name="XXX" value="c{$count}" />
   <xsl:if test="$count &lt; $max">
      <xsl:call-template name="checkboxes">
         <xsl:with-param name="count" select="$count + 1" />
         <xsl:with-param name="max" select="$max" />
      </xsl:call-template>
   </xsl:if>
</xsl:template>

You call this template initially by setting the $max parameter to the
number of checkboxes you need:

  <xsl:call-template name="checkboxes">
     <xsl:with-param name="max" select="$num-checkboxes" />
  </xsl:call-template>

Using the Piez Method, you get a set of random nodes, which has to be
at least as big as the largest number of checkboxes you'll ever need.
You then select from that node set a number of nodes equal to the
number of checkboxes that you want.  Then you iterate over that set of
nodes, giving a checkbox for each.  Within the loop, you can use
position() to give you an incrementing count:

  <xsl:variable name="random-nodes" select="document('')//node()" />
  <xsl:for-each select="$random-nodes[position() &lt;= $num-checkboxes]">
     <input type="checkbox" name="XXX" value="c{position()}" />
  </xsl:for-each>

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



_________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


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




Current Thread
Keywords