• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xsl variable and xsl:if

 
shailja saxena
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i have an xml :
<?xml version="1.0" encoding="UTF-8"?>
<?xml:stylesheet type="text/xsl" href="wcnoncRegion.xsl"?>
<sam>
<data>
<record>
<sno>1</sno>
<country>IN</country>
<allocated>6</allocated>
<delivered>3</delivered>
<incident>1</incident>
<percentage>66.67</percentage>
<netPrg>0</netPrg>
</record>
<record>
<sno>2</sno>
<country>MY</country>
<allocated>4</allocated>
<delivered>2</delivered>
<incident>0</incident>
<percentage>50</percentage>
<netPrg>2</netPrg>
</record>
</data>
</sam>
For this xml i want to display the record node details in a table in the form 1st record in white background and 2ns row in gray background.
I tried the following stuff..but i don't think it's right..could neone give me some pointers please. I m very new to xslt.
<xsl:for-each select="record">
<xsl:variable name="number">"><xsl:value-of select="record/sno"/></xsl:variable>
<xsl:if test="$number mod 2 =0">
<tr class="white"></tr>
<p>white</p>
</xsl:if>
<xsl:if test="$number mod 2 != 0">
<tr class="gray"></tr>
<p>gray</p>
</xsl:if>
</xsl:for-each>

thanx in advance
Shailja
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<xsl:variable name="number">"><xsl:value-of select="record/sno"/></xsl:variable>
<xsl:if test="$number mod 2 != 0">

I am not sure if mod is a valid function,
atleast not according to this web-page.
http://www.w3schools.com/xsl/xsl_functions.asp
I have not cross-checked with the XPath/XSL standard...
However, in your case, if the value of the element
sno represents the order, then you could use
the position() function to acheive your result:
Something like :
<xsl:if test = "(position() * 2) = ./sno">
This test I believe will be true for even rows and
false for odd rows.
Also, since you have ONLY two posibilities (assuming
the values of sno are not decimal), you should probably
use a choose/when/otherwise construct instead of the if...
I would personally prefer:
<xsl:choose>
   <xsl:when test = "(position() * 2) = ./sno">
<!-- This is an even row, use white color -->
   </xsl:when>
   <xsltherwise>
<!-- This is an odd row, use gray color -->
   </xsltherwise>
</xsl:choose>
Hope this helps.
regds.
- madhav
[ November 28, 2003: Message edited by: Madhav Lakkapragada ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Madhav Lakkapragada:
I am not sure if mod is a valid function,
atleast not according to this web-page.

It's not a function, it's an operator of the expression language specified in XPath.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Lasse.
I didn't have the XPath page book-marked
to verify and sorry about the wrong terminology.
- madhav.........getting old!
 
shailja saxena
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Madhav ,
Thanx for the help but it's still not working. It still gives me both rows in gray.
Any other way ?
thanx,
Shailja
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the XSL you tried using "position() mod 2 = 0"?
The <xsl:variable> stuff failed because a variable's value can be set only once, I think.
 
Balaji Loganathan
author and deputy
Bartender
Posts: 3150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lasse Koskela:
The <xsl:variable> stuff failed because a variable's value can be set only once, I think.

It is true but actually we can assign new value to the existing variable, like how it is done above for-loop.
The reason why the above xsl is not working is due to xpath and wrong strings
Shailja wrote: <xsl:variable name="number">"><xsl:value-of select="record/sno"/></xsl:variable> which is wrong.
Shailja
Please try below xsl.(Its working fine in IE). Change xpath as needed.
 
shailja saxena
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works Balaji . Thanx a lot Thanx everybody
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic