for-loop urgent help pls
Hi,
I have a problem using for-loop in xsl:
My xml file and xsl files are given below and the question is also posted. can some one please help me
xml file:
---
<root>
<data1>
<data2>
</data2>
</data1>
<data1>
<data2>
</data2>
</data1>
<data1>
<data2>
</data2>
</data1>
</root>
xsl file:
---
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:variable name="nodeno"> 5 </xsl:variable>
<xsl:variable name="nodeno1"> 10 </xsl:variable>
<html>
<head><title>some title goes over here</title></head>
<xsl:text> blah blah .... </xsl:text>
<body>
<table1>
<tr>
<td> Name </td>
<xsl:for-each select="root/data1">
<xsl:if test="$nodeno >= position()">
<td><xsl:value-of select= "root/data1"/></td>
</xsl:if>
</xsl:for-each>
</tr>
</table1>
<tr>
<td> Name </td>
<xsl:for-each select="root/data1">
<xsl:if test="position() >= $nodeno2 and position() < $nodeno3>
<td><xsl:value-of select= "root/data1"/></td>
</xsl:if>
</xsl:for-each>
</tr>
<table2>
</table2>
QUESTION:
by using the above files, I am able to generate the data and it looks like
Table 1
--------------
Name | value
--------------
Name | value1
--------------
Table 2
-------------
Name |
-------------
Name |
-------------
I want to tell the xsl some how that
if ( nodeno = 5)
generate table1
else if
generate table2
endif
waiting for some replies...
Thanks in advance.
[2377 byte] By [
chiru123] at [2007-11-19 11:11:47]

# 1 Re: for-loop urgent help pls
I'am not quite sure I'am understanding you correctly, at the beginnig of your post you talk of problems with your for-each loop, you then post a snippet of your code, which does not at all matchup with the output you claim to be getting, and then go on to ask how to make an xsl:if that checks if $nodeno is 5.
There is a multitude of problems with your code, not the least of which is that half of it appears to be missing.
Your code contain references to variables $nodono2 and $nodeno3 which has not been declared.
Inside the for-each loop you cannot refer to data1 elements like this "root/data1". Inside the for-each the context changes to the current node in the set of nodes selected in the for-each select expression. To get the current data1 node inside the for-each You should write something like <xsl:value-of select="."/>
if ( nodeno = 5)
generate table1
else if
generate table2
endif
There is no 'else' in XSL, either use 2 complimentary xsl:if's or an xsl:choose. something like this
<xsl:choose>
<xsl:when test="$nodeno = 5">
genereate table1
</xsl:when>
<xsl:otherwise>
generate table2
</xsl:otherwise>
</xsl:choose>
And please if you want me to help you again, try to ask more concise and complete questions.
khp at 2007-11-10 3:27:24 >

# 2 Re: for-loop urgent help pls
Hi Khp,
first of all thankyou for your help, Just now i observed that I didn't declared some variables in the code which I explained to you, but actually I did mention those variables in my code and hence I was able to get some outputs. I tried using <xsl:choose> option before posting this question, but now i foundout why my loop wasn't working and that is because I did't use <xsl:otherwise> and this happened for me because I am new to xml and I don't know the syntax. Anyway once again thankyou for your help and I will try to keep consise postings from next time.
Thanks,
# 3 Re: for-loop urgent help pls
Hi khp,
Thankyou for your reply. I have some problem and please see my code below
Assume that all the parameters are declared and this is the code which i am trying to use for displaying the data
xsl file:
<table>
<tr>
<td class="label">Name</td>
<xsl:for-each select="Data/Info">
<xsl:if test="position() >= $nodeno2 and position() < $nodeno3">
<td><xsl:value-of select= "Common/Size"/></td>
</xsl:if>
</xsl:for-each>
</tr>
</table>
results:
----------
Name | value | value | value |
----------
Name | value | value | value |
----------
This looks fine if I have all the two row value data in my xml file. But I want to display only one row i have only one row value data in my xml file. With the above code I was able to do that but the end result is something like below.
----------
Name | value | value | value |
----------
Name |
----------
but I don't want to display the "Name" in the second row if don't have the data in my xml. Can you help in getting me some solution to this problem. Please post me if you have some questions or if you don't understand my question.
Thanks in advance,
# 4 Re: for-loop urgent help pls
Thankyou for your reply. I have some problem and please see my code below
I will do no such thing. If you want my help please post complete working samples that acctually illustrates your problem.
There is absolutly nothing in the code you posted that explains why you get two rows, the code you posted is a table with a single row. I don't understand why you ask me to look at somthing that doesn't have any relevance for your problem. Please don't do it again.
khp at 2007-11-10 3:30:24 >
