RE: [xsl] alternating bg colour in nested data

Subject: RE: [xsl] alternating bg colour in nested data
From: Andy Joslin <andy.joslin@xxxxxxxx>
Date: Wed, 16 Jan 2002 13:46:57 -0000
You can grab how deep a node is using count(ancestor::node())

then you could put this in a variable in your template like so:

<xsl:variable name="ancestor-count" select="count(ancestor::node())"/>

and return this as a comment in your output like so,

<xsl:comment>This node is <xsl:copy-of select="$ancestor-count"/>
deep.</xsl:comment>

As for the dots, it would depend on how you achieved this, but you could
either call a template n times or use css margins for indentation of the
text , etc.


cheers


Andy






-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of Andrew Welch
Sent: 16 January 2002 12:25
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] alternating bg colour in nested data


(please excuse multiple questions, but Ive split them up to make it easier
to read :)

Also,

I would like to output a '.' to indicate how deeply nested an element is.
For example, a group3 item would be output as

'. . . data'

At the moment, Im trying to do this by passing as a parameter the number of
dots to either the even or the odd template.  This is fine as I can work out
the number of dots based on the group name.  But if the group name isnt
known, is there a way count how 'deep' a node is from another node?

For example:

<node1>
  <node2/>
  <node2/>
  <startNode>
    <node3/>
    <node3/>
      <node4>
        <node5>
          <endNode/>
        </node5>
      </node4>
    </node3>
  </startNode>
</node1>

Here I would like the output to be:

....endNode

(four dots then endnode, as endnode is four deep from startnode)

Is there a piece of code that can do this, using only the structure of the
XML file and a known startNode?

Thanks once again,

andrew

===





-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of G. Ken Holman
Sent: Tuesday, January 15, 2002 9:18 PM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] alternating bg colour in nested data


At 2002-01-15 17:51 +0000, Andrew Welch wrote:
>Im trying to get alternating table row background colours from nested xml.
>...
>I currently get alternating row colours using this kind of template for
each
>group:
>
><xsl:template match="group1">
><xsl:variable name="position" select="position()" />

The above is redundant ...

><xsl:choose>
>   <xsl:when test="$position mod 2 = 1">

You could have used test="position() mod 2 = 1"

>     <xsl:call-template name="row_even"/>
>   </xsl:when>
>   <xsl:otherwise>
>     <xsl:call-template name="row_odd"/>
>   </xsl:otherwise>
></xsl:choose>
><xsl:apply-templates select="group2"/>
></xsl:template>
>
>Each template is virtually identical apart from the 'groupX' number.

I have a suggestion for that as well.

>Also,
>occasionally two adjacent rows share the same colour (where group changes
>and last elem pos was even)

You'll have to keep track of the running count of all rows generated.

Now I don't know what you are doing in each row, but the example below
shows how I keep track of the running row count in a variable that is
passed.

Note also how I was able to reuse the same template rather than making
numerous "groupX" templates ... I am assuming the structure is as regular
as you imply it is.  If there are no children of the given name, it isn't
an error, so asking for all possible children at each level will only give
you the children that are at that level.

I hope this helps.

............................. Ken



T:\ftemp>type andrew.xml
<group1>
   <data1/>
   <data2/>
   <group2>
     <data1/>
     <data2/>
   </group2>
   <group2>
     <data1/>
     <data2/>
     <group3>
       <data1/>
       <data2/>
     </group3>
     <group3>
       <data1/>
       <data2/>
       <group4>
         <data1/>
         <data2/>
       </group4>
     </group3>
   </group2>
</group1>

T:\ftemp>type andrew.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                 version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
   <table>
     <xsl:apply-templates select="group1"/>
   </table>
</xsl:template>


<xsl:template match="group1|group2|group3|group4">
<xsl:param name="last" select="0"/>
<xsl:choose>
   <xsl:when test="( $last + position() ) mod 2 = 1">
     <xsl:call-template name="row_even"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:call-template name="row_odd"/>
   </xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="group2|group3|group4">
   <xsl:with-param name="last" select="$last + last()"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template name="row_even">
   <tr bgcolor="#cccccc">
     <xsl:for-each select="*[not(starts-with(name(),'group'))]">
       <td><xsl:value-of select="concat(name(..),' ',name(.))"/></td>
     </xsl:for-each>
   </tr>
</xsl:template>

<xsl:template name="row_odd">
   <tr>
     <xsl:for-each select="*[not(starts-with(name(),'group'))]">
       <td><xsl:value-of select="concat(name(..),' ',name(.))"/></td>
     </xsl:for-each>
   </tr>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt andrew.xml andrew.xsl andrew.htm

T:\ftemp>type andrew.htm
<?xml version="1.0" encoding="utf-8"?>
<table>
<tr bgcolor="#cccccc">
<td>group1 data1</td>
<td>group1 data2</td>
</tr>
<tr>
<td>group2 data1</td>
<td>group2 data2</td>
</tr>
<tr bgcolor="#cccccc">
<td>group2 data1</td>
<td>group2 data2</td>
</tr>
<tr>
<td>group3 data1</td>
<td>group3 data2</td>
</tr>
<tr bgcolor="#cccccc">
<td>group3 data1</td>
<td>group3 data2</td>
</tr>
<tr>
<td>group4 data1</td>
<td>group4 data2</td>
</tr>
</table>


--
Training Blitz: 3-days XSLT/XPath, 2-days XSLFO - Feb 18-22, 2002
-               (Early-bird date for discounts is this Friday!)

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 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:            2002-01-18,02-11,12,13,15,18,21,
-                                03-11,14,15,18,19,04-08,09,10,12


 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


Current Thread