xsl-list
[Top] [All Lists]

RE: Another Alternate table-row background color question - Using filters

2005-05-30 14:26:52
Gowtham,


stylesheet will extract only some information (filter) from the XML and
create a table. I want the rows in this table to have alternate background
color. Because of the filter condition in the XSL, I cannot use the
position() to check for odd/even rows. I need to (essentially) check the
row number inserted into the HTML <TABLE>.

Here're 2 XSLT 1.0 options: split the transform into 2 stylesheets--the first picks out only the rows you want, and the second formats them into HTML, this time using position() to determine even/odd rows. Alternatively you could do this in 1 stylesheet if you're willingto use an extension function such as node-set(). The idea here is to create a variable containing the rows you want, then to iterate rows inthis variable, this time using position(). Something like so:

My mistake in the response (above). position() keeps in step with the position of a node wrt its position in a [selected] nodeset, not just its position in the original document. It's properties such as prior-sibling, etc, that don't change from their original values. The upshot being that you don't need node-set() for what you're trying to do.

The following XSL picks out just the nodes of interest, adding a 0/1 according as the picked-out node is even/odd in the new nodeset. The (position() - 1) business is just to translate position() to be 0-based. Just to verify, you may want to change the selection below to nodes that you know are even/odd in the input XML, and still see that they take on 0/1 attributes based on their position in the newly-selected nodeset.


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output indent="yes"/>

   <xsl:template match="/">
       <table>
<xsl:apply-templates select="/Codes/Code[Name = 30 or Name=50 or Name=60]"/>
       </table>
   </xsl:template>

   <xsl:template match="Code">
       <xsl:copy>
           <xsl:attribute name="color">
               <xsl:value-of select="(position() - 1) mod 2"/>
           </xsl:attribute>
           <xsl:copy-of select="*"/>
       </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--