ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> xsl-list
xsl-list
Re: [xsl] Problem With Different Colors for Alternate Rows
by Joerg Heinicke other posts by this author
Nov 6 2002 12:50AM messages near this date
RE: [xsl] Problem With Different Colors for Alternate Rows | Re: Finding the closest node (was Re: [xsl] inconsistent Preceding-Sibling behaviour when nested in For-Each
Hello Rechell,

you must change the templates a little bit:

<xsl:template match="/Food_List"> 
    <table> 
      <!-- selects only the first node in every node group --> 
      <xsl:apply-templates select="*[not(name(preceding-sibling::*[1]) = 
name(current())]" mode="row"/> 
    </table> 
</xsl:template> 

<xsl:template match="*" mode="row"> 
    <TR class="evenRowStyle"> 
      <xsl:if test="position() mod 2 = 1"> 
        <xsl:attribute name="class"> oddRowStyle</xsl:attribute>
      </xsl:if> 
      <xsl:apply-templates select="." mode="cell"/> 
    </TR> 
</xsl:template> 

<xsl:template match="Side_Dish" mode="cell"> 
    <TD> Side Dish:</TD>
    <TD> <xsl:apply-templates select="."/></TD>
</xsl:template> 

<xsl:template match="Side_Dish"> 
   <xsl:value-of select="."/> 
   <!-- maybe more understandable, but I prefer the second one:
           test="name(following-sibling::*[1] = 'Side_Dish')" --> 
   <xsl:if test="following-sibling::*[1][self::Side_Dish]"> 
     <xsl:text> , </xsl:text>
     <xsl:apply-templates select="following-sibling::*[1]"/> 
   </xsl:if> 
</xsl:template> 

For the other elements (they had additional child elements) it's 
similar, should not be so difficult to adapt the logic.

And please never use disable-output-escaping="yes" if you want to create 
elements. It's obviously, with <xsl:text/>  text will be created, but not 
elements. d-o-e should only be used for non-XML-conform things like JSP 
and PHP stuff. Search the archive for "d-o-e" or 
"disable-output-escaping". You will find many threads and postings.

Regards,

Joerg

Schwartz, Rechell R, ALCAS wrote:
>  Jeorg,
>  
>  Thank you. Mode seems to be working ok, except for one thing -- in my
>  real XSLT (which is more complicated than the sample below), for certain
>  data elements (e.g., side dish) I search for multiple occurences of the
>  element and I concatenate them with commas as below. This is causing
>  confusion in the assessment of the position() function. It seems to
>  think I am looking for the position of the row, when I am really looking
>  for the position of this particular side dish. This confusion is causing
>  each side dish to print out on a new line instead of on a single line
>  with a comma-separated list. Is there any way to test if this side dish
>  is first or last w/o using the position() function (thus avoiding this
>  type of confusion)?
>  
>  <xsl:template match="Side_Dish">
>  <xsl:if test="position() = 1">
>  	<xsl:text disable-output-escaping="yes">&lt;td
>  class="oddRowStyle"&gt;Side Dish:&lt;td
>  class="oddRowStyle"&gt;</xsl:text>
>  </xsl:if>
>        <xsl:value-of select="."/>
>  <xsl:choose>
>  	<xsl:when test="position() != last()">
>   		<xsl:text>, </xsl:text>
>  	</xsl:when>
>  	<xsl:otherwise>
>  		<xsl:text
>  disable-output-escaping="yes">&lt;/td&gt;</xsl:text>
>  	</xsl:otherwise>
>  </xsl:choose>
>  </xsl:template>
>  
>  Thanks,
>  Rechell Schwartz
>  
>  -----Original Message-----
>  From: Joerg Heinicke [mailto:joerg.heinicke@[...].de]
>  Sent: Tuesday, November 05, 2002 1:11 AM
>  To: xsl-list@[...].com
>  Subject: Re: [xsl] Problem With Different Colors for Alternate Rows
>  
>  
>  Hello Rechell,
>  
>  there is also a simple XSL way:
>  
>  <xsl:template match="/Food_List">
>     <table>
>       <xsl:apply-templates select="Main_Food_List | Side_Dish | Desert" 
>  mode="row"/>
>     </table>
>  </xsl:template>
>  
>  <xsl:template match="*" mode="row">
>     <TR class="evenRowStyle">
>       <xsl:if test="position() mod 2 = 1">
>         <xsl:attribute name="class">oddRowStyle</xsl:attribute>
>       </xsl:if>
>       <xsl:apply-templates select="."/>
>     </TR>
>  </xsl:template>
>  
>  <xsl:template match="Main_Food_List">
>     <TD>Main Course:</TD>
>     <TD><xsl:value-of select="Main_Course"/></TD>
>  </xsl:template>
>  
>  <xsl:template match="Side_Dish">
>     <TD>Side Dish:</TD>
>     <TD><xsl:value-of select="."/></TD>
>  </xsl:template>
>  
>  <xsl:template match="Desert">
>     <TD>Desert:</TD>
>     <TD><xsl:value-of select="Fancy_Desert"/></TD>
>  </xsl:template>
>  
>  Regards,
>  
>  Joerg
>  
>  Schwartz, Rechell R, ALCAS wrote:
>  
> >All,
> >
> >I am having difficulty programming a stylesheet that is supposed to
>  
>  return the result set as a list of HTML rows in alternating colors.All
>  of the examples that I have seen address how to do this if every data
>  row is is derived from the same type of XML tag. But in my example, each
>  row may come from a completely different section of the XML. Following
>  is a sample XML similar to the one that I am using. Also, another
>  constraint is if any row is missing, it is inhibited, and the
>  alternating color rule must still be preserved. I have tried numerous
>  ways of passing parameters, but have been unable to modify the the row
>  count after a match was found ofr each item. Any help would be GREATLY
>  appreciated.
>  
> >Sample XML:
> >
> ><Food_List>
> ><Main_Food_List>
> ><Main_Course>
> >Chicken
> ></Main_Course>
> ></Main_Food_List>
> ><Side_Dish>
> >Potatoes
> ></Side_Dish>
> ><Desert>
> ><Fancy_Desert>
> >Chocolate Truffles
> ></Fancy_Desert>
> ></Desert>
> ></Food_List>
> >
> >Sample XSLT Stylesheet:
> ><?xml version="1.0" ?>
> ><xsl:stylesheet version='1.0'
>  
>  xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
>  
> ><xsl:output method="html" indent="yes"/>
> ><xsl:template match="/">
> ><xsl:apply-templates select="Food_List/Main_Food_List/Main_Course"/>
> ><xsl:apply-templates select="Food_List/Side_Dish"/>
> ><xsl:apply-templates select="Food_List/Desert/Fancy_Desert"/>
> >
> ><xsl:template match="Main_Course">
> ><TR><TD>Main Course:</TD><TD xsl:value-of select="."/></TD></TR>
> ></xsl:template>
> >
> ><xsl:template match="Side_Dish">
> ><TR><TD>Side Dish:</TD><TD xsl:value-of select="."/></TD></TR>
> ></xsl:template>
> >
> ><xsl:template match="Fancy_Desert">
> ><TR><TD>Desert:</TD><TD xsl:value-of select="."/></TD></TR>
> ></xsl:template>
> >
> ></xsl:stylesheet>
> >
> >
> >
> >Required HTML Output
> ><TABLE>
> ><TR><TD colspan="2">MENU</TD></TR>
> ><TR><TD class="oddRowStyle">Main Dish:</TD><TD
>  
>  class="oddRowStyle">Chicken</TD></TR>
>  
> ><TR><TD class="evenRowStyle">Side Dish:</TD><TD
>  
>  class="oddRowStyle">Potatoes</TD></TR>
>  
> ><TR><TD class="oddRowStyle">Desert:</TD><TD
>  
>  class="oddRowStyle">Chocolate Truffles</TR>
>  
> ></TABLE>
>  
>  
>  
>   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
Thread:
Schwartz, Rechell R, ALCAS
Joerg Heinicke

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved