XML Development

Moderators: None (Apply to moderate this forum)
Number of threads: 252
Number of posts: 451

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem with XSL loop Posted by hellsgate on 20 Feb 2004 at 7:45 AM
Hi,
I've not been working with XML/XSL for long and I've hit a snag. I'm trying to fill out a table with info from my XML. Usually, I'd use 'xsl:for-each', but on this occasion I can't. Here is the XML:


<root>
<test>
<data>
<client>
<name>Brian</name>
<age>28</age>
<colour>Red</colour>
<smell>Pizza</smell>
</client>
<client>
<name>Steven</name>
<age>42</age>
<colour>Cerise</colour>
<smell>Baby poo</smell>
</client>
<client>
<name>Raymond</name>
<age>2</age>
<colour>Blue</colour>
<smell>Fish guts</smell>
</client>
<client>
<name>Tam</name>
<age>35</age>
<colour>Green</colour>
<smell>Beef crisps</smell>
</client>
</data>
</test>
</root>


And here is the XSL transform I have so far:


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<form action="" method="post">
<table width="740" border="1" cellspacing="0" cellpadding="2" align="center">
<xsl:variable name="counter" select="1"/>
<xsl:variable name="nodecount"><xsl:value-of select="count(//root//test//data//client)"/></xsl:variable>
<tr>
<td>Name</td>
<td>Age</td>
<td>Colour</td>
<td>Smell</td>
<td>Select</td>
</tr>
<xsl:call-template name="table">
<xsl:with-param name="counter"/>
<xsl:with-param name="nodecount"/>
</xsl:call-template>
</table>
</form>
</xsl:template>
<xsl:template name="table">
<xsl:param name="counter"/>
<xsl:param name="nodecount"/>
<xsl:if test="$counter &lt;=$nodecount">
<tr>
<td width="148"><xsl:value-of select="root/test/data/client/name"/></td>
<td width="148"><xsl:value-of select="root/test/data/client/age"/></td>
<td width="148"><xsl:value-of select="root/test/data/client/colour"/></td>
<td width="148"><xsl:value-of select="root/test/data/client/smell"/></td>
<td width="148"><input name="selectperson" type="checkbox" value=""/>Tick here to select</td>
</tr>
<xsl:call-template name="table">
<xsl:with-param name="counter"><xsl:value-of select="$counter+1"/></xsl:with-param>
<xsl:with-param name="nodecount" select="$nodecount"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

Any help would be greatly appreciated. Thanks in advance

Hellza
Report
Re: Problem with XSL loop Posted by infidel on 2 Mar 2004 at 12:09 PM
: Hi,
: I've not been working with XML/XSL for long and I've hit a snag. I'm trying to fill out a table with info from my XML. Usually, I'd use 'xsl:for-each', but on this occasion I can't. Here is the XML:
:
:
: <root>
: <test>
: <data>
: <client>
: <name>Brian</name>
: <age>28</age>
: <colour>Red</colour>
: <smell>Pizza</smell>
: </client>
: <client>
: <name>Steven</name>
: <age>42</age>
: <colour>Cerise</colour>
: <smell>Baby poo</smell>
: </client>
: <client>
: <name>Raymond</name>
: <age>2</age>
: <colour>Blue</colour>
: <smell>Fish guts</smell>
: </client>
: <client>
: <name>Tam</name>
: <age>35</age>
: <colour>Green</colour>
: <smell>Beef crisps</smell>
: </client>
: </data>
: </test>
: </root>
:
:
: And here is the XSL transform I have so far:
:
:
: <?xml version="1.0"?>
: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
: <xsl:template match="/">
: <form action="" method="post">
: <table width="740" border="1" cellspacing="0" cellpadding="2" align="center">
: <xsl:variable name="counter" select="1"/>
: <xsl:variable name="nodecount"><xsl:value-of select="count(//root//test//data//client)"/></xsl:variable>
: <tr>
: <td>Name</td>
: <td>Age</td>
: <td>Colour</td>
: <td>Smell</td>
: <td>Select</td>
: </tr>
: <xsl:call-template name="table">
: <xsl:with-param name="counter"/>
: <xsl:with-param name="nodecount"/>
: </xsl:call-template>
: </table>
: </form>
: </xsl:template>
: <xsl:template name="table">
: <xsl:param name="counter"/>
: <xsl:param name="nodecount"/>
: <xsl:if test="$counter &lt;=$nodecount">
: <tr>
: <td width="148"><xsl:value-of select="root/test/data/client/name"/></td>
: <td width="148"><xsl:value-of select="root/test/data/client/age"/></td>
: <td width="148"><xsl:value-of select="root/test/data/client/colour"/></td>
: <td width="148"><xsl:value-of select="root/test/data/client/smell"/></td>
: <td width="148"><input name="selectperson" type="checkbox" value=""/>Tick here to select</td>
: </tr>
: <xsl:call-template name="table">
: <xsl:with-param name="counter"><xsl:value-of select="$counter+1"/></xsl:with-param>
: <xsl:with-param name="nodecount" select="$nodecount"/>
: </xsl:call-template>
: </xsl:if>
: </xsl:template>
:
: </xsl:stylesheet>
:
: Any help would be greatly appreciated. Thanks in advance

What is the problem and why can't you use an xsl:for-each?


infidel

$ select * from users where clue > 0
no rows returned


Report
Re: Problem with XSL loop Posted by hellsgate on 3 Mar 2004 at 2:40 AM
Thanks for replying Infidel,

I couldn't use xsl:for-each as the table I was building was inside a template. However, I've managed to get it running ok now, so once again, thanks for taking the time to have a look at the problem in the first place.

Hellsgate
Report
Re: Problem with XSL loop Posted by infidel on 3 Mar 2004 at 7:58 AM
: Thanks for replying Infidel,
:
: I couldn't use xsl:for-each as the table I was building was inside a template. However, I've managed to get it running ok now, so once again, thanks for taking the time to have a look at the problem in the first place.

I don't understand why xsl:for-each wouldn't work for you, but if it's working now then that's what counts.


infidel

$ select * from users where clue > 0
no rows returned





 

Recent Jobs