Re: [xsl] every tenth row change bgcolor

Subject: Re: [xsl] every tenth row change bgcolor
From: Jarkko Moilanen <Jarkko.Moilanen@xxxxxx>
Date: Fri, 6 Jun 2003 15:14:50 +0300 (EEST)
On Fri, 6 Jun 2003, G. Ken Holman wrote:

> Good morning, Jarkko!
>
> At 2003-06-06 13:20 +0300, Jarkko Moilanen wrote:
> >1. The question is that why in the first group is only 9 rows?
> >
> >2. In this part : (floor(position() div 20) mod 2),
> >    Am I saying something like modulate the group of
> >    20 by 0 and 1 values? Quess not, but have to suggest something =)
>
> There were a number of problems with your original stylesheet that combined
> to give you the problems you were having.
>
> (1) - counting in XSLT begins at 1, so at the least, your calculation has
> to deal with "(position() - 1)" instead of "position()" when doing modulo
> arithmetic
>
> (2) - I was confused why you were using modulo 20 instead of 10 when your
> groups were of 10, but then I realized you were using
> <xsl:apply-templates/> instead of <xsl:apply-templates
> select="Report/Row"/> ... the difference is that without a select=
> attribute, *all* child nodes are processed, thus processing the intervening
> text nodes as well as the <Row> nodes.  It happened that between
> your  groups of 10 you had comments which shifted your "child node" count by 1
>
> So, you will see below a working version where I tighten up the node
> processing by pushing only the nodes that are to be affected by the
> position() function in the current node list, and the counting modulo
> arithmetic is based on the grouping of 10 Rows, not the combination of 10
> Rows and 10 Text nodes.
>
> I hope this helps.
>
> ............... Ken

Yes, this was a great answer, thank you. =)

>
> p.s. remember the exercise of counting the node children and neglecting to
> count the text nodes at the end of each line?

Now that you brought it up...yes =)

I mentioned how this is a
> *very* common mistake made by most users of XSLT.
>
> T:\ftemp>type jarkko.xml
> <?xml version="1.0" encoding="UTF-8"?>
> <Report>
>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row> <!-- 10 -->
>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row>
>   <Row>blue</Row> <!-- 20 -->
>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row>
>   <Row>white</Row> <!-- 30 -->
>
> </Report>
>
> T:\ftemp>type jarkko.xsl
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="html"/>
> <!-- Root template of my stylesheet -->
>     <xsl:template match="/">
>       <html>
>         <head>
>            <title>Color rows</title>
>          <style type="text/css">
>          .r0 {background-color: white}
>          .r1 {background-color: blue}
>          </style>
>         </head>
>         <body>
>          <table border="1">
>            <xsl:apply-templates select="Report/Row"/>
>          </table>
>         </body>
>       </html>
>     </xsl:template>
>
>     <xsl:template match="Row">
>       <tr class="r{floor((position() - 1) div 10) mod 2}">
>         <xsl:apply-templates/>
>       </tr>
>     </xsl:template>
>
> </xsl:stylesheet>
>
> T:\ftemp>saxon -o jarkko.htm jarkko.xml jarkko.xsl
>
> T:\ftemp>type jarkko.htm
> <html>
>     <head>
>        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
>
>        <title>Color rows</title><style type="text/css">
>          .r0 {background-color: white}
>          .r1 {background-color: blue}
>          </style></head>
>     <body>
>        <table border="1">
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r1">blue</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>           <tr class="r0">white</tr>
>        </table>
>     </body>
> </html>
> T:\ftemp>rem Done!
>
> --
> Upcoming hands-on courses: (registration still open!)
> -      (XSLT/XPath and/or XSL-FO) North America: June 16-20, 2003
>
> G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
> Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
> Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
> ISBN 0-13-065196-6                      Definitive XSLT and XPath
> ISBN 0-13-140374-5                              Definitive XSL-FO
> ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
> ISBN 1-894049-11-X              Practical Formatting Using XSL-FO
> Member of the XML Guild of Practitioners:    http://XMLGuild.info
> Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>


****************************************************************
Jarkko Moilanen          "Erehtyminen on inhimillista,
Researcher                mutta todella suuret mokat
jm60697@xxxxxx            vaativat tietokoneen käyttöä."
www.uta.fi/~jm60697
GSM: +358 50 3766 927
****************************************************************
* ITCM | Information Technology and Crisis Management
* http://www.itcm.org
****************************************************************






 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread