aspose file tools*
The moose likes XML and Related Technologies and the fly likes xsl variable and xsl:if Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xsl variable and xsl:if" Watch "xsl variable and xsl:if" New topic
Author

xsl variable and xsl:if

shailja saxena
Greenhorn

Joined: Nov 28, 2003
Posts: 5
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

Joined: Jun 03, 2000
Posts: 5040
<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 ]

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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

Joined: Nov 28, 2003
Posts: 5
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jul 13, 2001
Posts: 3150
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

Joined: Nov 28, 2003
Posts: 5
This works Balaji . Thanx a lot Thanx everybody
 
 
subject: xsl variable and xsl:if