Re: [xsl] XSLT Problem

Subject: Re: [xsl] XSLT Problem
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Wed, 20 Jun 2001 17:18:00 -0400
I can't see why you have a problem with this.  There must be some other
factor I haven't noticed.  Also, I don't see why you do a test for a field-C
value of ' ', then bother to do a select on it when you already know what it
is.

I wrote a simplified xml file that seems to be essentially what you have.  I
added an attribute to each record to designate the row's original order, to
make tracking the results easier.  The table rows come out colored
alternately as desired, after sorting.  I applied the color in the <td>, but
obviously you could assign a class and use CSS as you did.  Here are the xml
and xslt files:

================ XML ==========================
<doc>
 <rec n='1'>
      <a>A</a>
      <b>B</b>
      <c>C</c>
 </rec>
 <rec n='2'>
      <a>mA</a>
      <b>bB</b>
      <c>fC</c>
 </rec>
 <rec n='3'>
      <a>bA</a>
      <b>aB</b>
      <c>qC</c>
 </rec>
 <rec n='4'>
      <a>hA</a>
      <b>fB</b>
      <c>zC</c>
 </rec>
 </doc>
================ XSLT ==========================
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method='html'/>

<xsl:template match="/doc">
<html>
<table border='1'>
 <xsl:for-each select='rec'>
  <xsl:sort select='a'/>
  <xsl:sort select='b'/>

  <!--========= Calculate the row's color ========-->
  <xsl:variable name='color'>
   <xsl:choose>
        <xsl:when test='position() mod 2=0'>lightblue</xsl:when>
        <xsl:otherwise>palegoldenrod</xsl:otherwise>
   </xsl:choose>
  </xsl:variable>

  <tr><td>Original Row position: <xsl:value-of select='@n'/></td>
  <xsl:apply-templates>
    <xsl:with-param name='color' select='$color'/>
   </xsl:apply-templates>
  </tr>
 </xsl:for-each>
</table>
</html>
</xsl:template>

<!--========== Write each row =========-->
<xsl:template match='a|b|c'>
 <xsl:param name='color'/>
 <td bgcolor='{$color}'><xsl:value-of select='.'/></td>
</xsl:template>

</xsl:stylesheet>

============= HTML output ====================
<html>
<table border="1">
<tr><td>Original Row position: 1</td>
<td bgcolor="palegoldenrod">A</td>
<td bgcolor="palegoldenrod">B</td>
<td bgcolor="palegoldenrod">C</td>
</tr>
<tr><td>Original Row position: 3</td>
<td bgcolor="lightblue">bA</td>
<td bgcolor="lightblue">aB</td>
<td bgcolor="lightblue">qC</td>
</tr>
<tr><td>Original Row position: 4</td>
<td bgcolor="palegoldenrod">hA</td>
<td bgcolor="palegoldenrod">fB</td>
<td bgcolor="palegoldenrod">zC</td>
</tr>
<tr><td>Original Row position: 2</td>
<td bgcolor="lightblue">mA</td>
<td bgcolor="lightblue">bB</td>
<td bgcolor="lightblue">fC</td>
</tr>
</table>
</html>

===============================================

Cheers,

Tom P

[Alex Genis]
> My task is : to create HTML report from XML document and to color every
other
> row  for the result HTML table.
> Everything is already done but I still have one problem:
>
> If I use
>
>   <xsl:for-each select="Record01">
>    <xsl:sort select="field-A"/>
>    <xsl:sort select="field-B"/>
>
>      <xsl:if test="field-C = '  '" >
>         <TR>
>           <xsl:choose>
>            <xsl:when test="position() mod 2 = 0">
>              <TD   ...   class="C1">
>                                        <xsl:value-of select="field-A"/>
>              </TD>
>              <TD   ...   class="C1">
>                                        <xsl:value-of select="field-B"/>
>              </TD>
>              <TD   ...   class="C1">
>                                        <xsl:value-of select="field-C"/>
>              </TD>
>            </xsl:when>
>            <xsl:otherwise>
>              <TD   ...   class="C2">
>                                        <xsl:value-of select="field-A"/>
>              </TD>
>              <TD   ...   class="C2">
>                                        <xsl:value-of select="field-B"/>
>              </TD>
>              <TD   ...   class="C2">
>                                        <xsl:value-of select="field-C"/>
>              </TD>
>            </xsl:otherwise>
>         </xsl:choose>
>       </TR>
>    </xsl:if>
>
> </xsl:for-each>
>
> I've got result table where I can not color every other row because as I
> understand the system :
> 1. retrieves rows and sorts them by appropriate keys;
>    for example :
>      1-st row   (value of C-field is '  ') - black;
>      2-nd row (value of C-field is '11') - gray;
>      3-rd row  (value of C-field is '  ') - black;
>      4-th row   (value of C-field is '22') - gray;
>      5-th row   (value of C-field is '  ') - black;
>      6-th row   (value of C-field is '  ') - gray;
>
> 2. and only then excludes 2-nd and 4-th rows( where field-C not = '  ')
>
> So instead of sequence :
>
> 1-st row (value of C-field is '  ') - black;
> 3-rd row (value of C-field is '  ') - gray;
> 5-th row (value of C-field is '  ') - black;
> 6-th row (value of C-field is '  ') - gray
>
> I've got :
>
> 1-st row (value of C-field is '  ') - black;
> 3-rd row (value of C-field is '  ') - black;
> 5-th row (value of C-field is '  ') - black;
> 6-th row (value of C-field is '  ') - gray
>
> Or other words does XSLT give us the posibility to use something like
"where" in
>  DB2 ?
> And if it does - how to use it within <xsl:for-each> or
<xsl:apply-templates
> select="..."> BEFORE SORTING ?
>
> How this problem can be solved ?
> Thanks a lot. Alex Genis.
>
>
>
>
>
>
>
>
>
>
>
>
>
>  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