This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: doubt regarding position() mod 2
- From: "Anand Ganapathy" <Anand dot Ganapathy at cgi dot ca>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Tue, 23 Apr 2002 17:26:19 +0530
- Subject: RE: [xsl] doubt regarding position() mod 2
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Michael,
I ought to have sent in my stylesheet too.Please find the code below.
MY XSL:
/*/*/*/*/*/*/*/*/
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<!-- match for the Root Element of the XML Document -->
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="Node1">
<!-- Check if this Node has Children -->
<!-- Display "Node1" tag -->
<table width="100%" cellspacing="10" >
<tr>
<td>
<table cellpadding="1" width="100%">
<TR>
<TH>PRODUCT HIERARCHY</TH>
</TR>
<TR>
<TD>
<xsl:value-of select="Nodename"/>
</TD>
</TR>
<xsl:variable name="j" select="1"> </xsl:variable>
<xsl:variable name = "count1">
<xsl:value-of select = "count(//Node2)"/>
</xsl:variable>
<xsl:call-template name = "forloop1" >
<xsl:with-param name = "j" select = "$j"/>
<xsl:with-param name = "count1" select = "$count1"/>
</xsl:call-template>
<TR>
<TD><br/></TD>
</TR>
<TR >
<TD align="center"><input type="button" value="Show Items" onClick="javascript:showItems();" /></TD>
</TR>
<TR >
<TD align="center"><input type="button" value="Select Hierarchy" onClick="javascript:selectHierarchy();" /></TD>
</TR>
</table>
</td>
</tr>
</table>
</xsl:template>
<!-- Anand Forloop1-->
<xsl:template name= "forloop1">
<xsl:param name = "j"/>
<xsl:param name = "count1"/>
<!-- Check if this Node has Children -->
<xsl:variable name="i" select="1"> </xsl:variable>
<xsl:variable name = "count2">
<xsl:value-of select = "count(//Node2[$j]/Node3) "/>
</xsl:variable>
<!-- For "Node2" tag -->
<xsl:if test="($count1 > 0)">
<xsl:if test="position() mod 2">
<TR bgcolor="blue">
<TD>
<xsl:value-of select="//Node2[$j]/Nodename"/>
</TD>
</TR>
</xsl:if>
<xsl:if test="not(position() mod 2)">
<TR bgcolor="red">
<TD>
<xsl:value-of select="//Node2[$j]/Nodename"/>
</TD>
</TR>
</xsl:if>
</xsl:if>
<xsl:call-template name = "forloop2" >
<xsl:with-param name="i" select="$i" />
<xsl:with-param name="j" select="$j" />
<xsl:with-param name="count2" select="$count2" />
<xsl:with-param name="count1" select="$count1" />
</xsl:call-template>
<xsl:if test="$j < $count1">
<xsl:call-template name="forloop1">
<xsl:with-param name="j" select="$j+1"/>
<xsl:with-param name="count1" select="$count1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Anand Forloop2-->
<xsl:template name = "forloop2" >
<xsl:param name="i" />
<xsl:param name="j" />
<xsl:param name = "count2" />
<xsl:param name = "count1"/>
<xsl:variable name="h" select="1"> </xsl:variable>
<xsl:variable name = "count3">
<xsl:value-of select = "count(//Node2[$j]/Node3[$i]/Node4)"/>
</xsl:variable>
<!-- For "Node3" tag -->
<xsl:if test="($count1 > 0)">
<xsl:if test="($count2 > 0)">
<TR>
<TD>
<xsl:value-of select="//Node2[$j]/Node3[$i]/Nodename"/>
</TD>
</TR>
</xsl:if>
</xsl:if>
<xsl:call-template name = "forloop3" >
<xsl:with-param name="h" select="$h" />
<xsl:with-param name="i" select="$i" />
<xsl:with-param name="j" select="$j" />
<xsl:with-param name="count3" select="$count3" />
<xsl:with-param name="count2" select="$count2" />
<xsl:with-param name="count1" select="$count1" />
</xsl:call-template>
<xsl:if test="($i < $count2)">
<xsl:call-template name="forloop2">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="j" select="$j"/>
<xsl:with-param name="count2" select="$count2"/>
<xsl:with-param name="count1" select="$count1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Anand Forloop3-->
<xsl:template name = "forloop3" >
<xsl:param name="h" />
<xsl:param name="i" />
<xsl:param name="j" />
<xsl:param name = "count3" />
<xsl:param name = "count2" />
<xsl:param name = "count1"/>
<!-- For "Node4" tag -->
<xsl:if test="($count1 > 0)">
<xsl:if test="($count2 > 0)">
<xsl:if test="($count3 > 0)">
<TR>
<TD>
<xsl:value-of select="//Node2[$j]/Node3[$i]/Node4[$h]/Nodename"/>
</TD>
</TR>
</xsl:if>
</xsl:if>
</xsl:if>
<xsl:if test="$h < $count3">
<xsl:call-template name="forloop3">
<xsl:with-param name="h" select="$h+1"/>
<xsl:with-param name="i" select="$i"/>
<xsl:with-param name="j" select="$j"/>
<xsl:with-param name="count3" select="$count3"/>
<xsl:with-param name="count2" select="$count2"/>
<xsl:with-param name="count1" select="$count1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
My XML
/*/*/*/*/*/*/*/*/*/*
<?xml version="1.0" encoding="UTF-8"?>
<NODE1>
<NODEADDRESS>1</NODEADDRESS>
<LEVELIDENTIFIER>1</LEVELIDENTIFIER>
<NODENAME>All Products </NODENAME>
<NODE2>
<NODEADDRESS>1:1</NODEADDRESS>
<LEVELIDENTIFIER>2</LEVELIDENTIFIER>
<NODENAME>Meat </NODENAME>
<NODE3>
<NODEADDRESS>1:1:1</NODEADDRESS>
<LEVELIDENTIFIER>3</LEVELIDENTIFIER>
<NODENAME>Eggs </NODENAME>
</NODE3>
<NODE3>
<NODEADDRESS>1:1:2</NODEADDRESS>
<LEVELIDENTIFIER>3</LEVELIDENTIFIER>
<NODENAME>Red Meat </NODENAME>
</NODE3>
</NODE2>
<NODE2>
<NODEADDRESS>1:2</NODEADDRESS>
<LEVELIDENTIFIER>2</LEVELIDENTIFIER>
<NODENAME>Dairy </NODENAME>
</NODE2>
<NODE2>
<NODEADDRESS>1:3</NODEADDRESS>
<LEVELIDENTIFIER>2</LEVELIDENTIFIER>
<NODENAME> Chocolate </NODENAME>
</NODE2>
</NODE1>
Thanks for your help.
AnandG.
>>> "Michael Kay" <michael.h.kay@ntlworld.com> 04/23/02 04:57PM >>>
I don't usually try to debug people's stylesheets unless I can see them.
This time I tried and it seems I guessed wrong. Your next step is to show us
your code.
Michael Kay
Software AG
home: Michael.H.Kay@ntlworld.com
work: Michael.Kay@softwareag.com
> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com
> [mailto:owner-xsl-list@lists.mulberrytech.com]On Behalf Of Anand
> Ganapathy
> Sent: 23 April 2002 09:34
> To: xsl-list@lists.mulberrytech.com
> Subject: RE: [xsl] doubt regarding position() mod 2
>
>
> Hello Michael,
> Thanks. I used <xsl:strip-space elements="*"/> as suggested
> by you but it doesn't work.Am I missing out on something
> else.Would you suggest any other approach to do the same?
> Anand
>
> >>> "Michael Kay" <michael.h.kay@ntlworld.com> 04/23/02 01:27PM >>>
> > In my XSL, I tried using a test condition <xsl:if
> > test="position() mod 2 = 1">,
> > This test condition should return true for any item element
> > that is an odd-numbered item child of its parent and hence
> > 'Meat' and 'Chocolate' should be diplayed in the same color
> > whereas 'Dairy' should be in a different color.
> > However, I am getting all these in the same color.
>
> You probably forgot to strip out the whitespace text nodes,
> which means that
> the children of an element will have whitespace text nodes at
> odd numbered
> positions and element nodes at even numbered positions.
>
> Use <xsl:strip-space elements="*"/>
>
> Michael Kay
> Software AG
> home: Michael.H.Kay@ntlworld.com
> work: Michael.Kay@softwareag.com
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list