[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] xsl:sort question: sorting element based upon particular child node


Subject: RE: [xsl] xsl:sort question: sorting element based upon particular child node
From: Américo Albuquerque <melinor@xxxxxxx>
Date: Thu, 14 Aug 2003 00:59:48 +0100

Hi.

> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Bruce Rojas-Rennke
> Sent: Wednesday, August 13, 2003 10:59 PM
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] xsl:sort question: sorting element based upon 
> particular child node
> 
> 
> Hello all,
> 
>   I have a sorting prb, - I have to put all the 'ColData' 
> elements for each 'DataRow' into a row like so...
> 
> 2003-07-01  70103     AMS Courier      2003-08-15  0   27.50
> 2003-07-23  29190087  Aegis Staffing   2003-08-22  0  260.00
> 2003-00-01  93        Boulder CC       2003-09-01  0   70.00
> 2003-05-13  70103     Yellow Book USA  2003-06-12  0  285.00
> 
>   In short, I have to spit out all the 'DataRows''ColData' elements,
> - sorted upon the '@value' attribute of only those ColData 
> elements which have an '@colID' = '4'.
> 
>   Or put another way, I have to sort on element(DataRow) and 
> it's child nodes(all named ColData), by evaluating one 
> attribute(ColData/@value) based upon the value(4) of another 
> attribute(ColData/@colID).

Not exacly, you want to sort on element (DataRow) by it's child ColData,
that has an colID equal to 4, value attribute
So, you'll have to use <xsl:sort select="ColData[@colID=4]/@value"/>

> 
>   The crux of my prb, as I see it, is that all the child 
> nodes of node 'DataRow' have the same bloody name (ColData).
> 
> 
> Here is new test code: XSLT
> (OK for the one ColData (@colID='4'),
>  but not all the ColData elements)
> *****************************************
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
>                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="ReportRet">
>   <table>
>     <xsl:for-each select="//DataRow/ColData[@colID='4']">
  
      // is very expensive, you'll mit just well use ReportData instead
      you don't want ColData here. Here you are creating <tr>s, so change
this to
      <xsl:for-each select="ReportData/DataRow">

>       <xsl:sort select="@value" data-type="text" />

        here you use the sort mentioned before instead
        <xsl:sort select="ColData[@colID=4]/@value"/>

>     <tr>

        Now you'll build each <td>
        <xsl:for-each select="ColData">

>       <td>
>         <xsl:value-of select="@value"/>
>       </td>
        </xsl:for-each>
>     </tr>
>       </xsl:for-each>
>     </table>
>   </xsl:template>
> </xsl:stylesheet>
> 

The actual code will look like:

  <xsl:template match="ReportRet">
    <table>
      <xsl:for-each select="ReportData/DataRow">
        <xsl:sort select="ColData[@colID=4]/@value"/>
        <tr>
          <xsl:for-each select="ColData">
            <td>
              <xsl:value-of select="@value"/>
            </td>
          </xsl:for-each>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

(...)

You could also use templates, something like:

  <xsl:template match="ReportRet">
    <table>
      <xsl:apply-templates select="ReportData"/>
    </table>
  </xsl:template>
  <xsl:template match="ReportData">
    <xsl:apply-templates select="DataRow">
      <xsl:sort select="ColData[@colID=4]/@value"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="DataRow">
    <tr>
      <xsl:apply-templates select="ColData"/>
    </tr>
  </xsl:template>
  <xsl:template match="ColData">
    <td>
      <xsl:value-of select="@value"/>
    </td>
  </xsl:template>

Both stylesheets applyied to your input data results on:

2003-07-23  29190087  Aegis Staffing Services, Inc.  2003-08-22  0   260.00
2003-07-01  70103     AMS Courier                    2003-08-15  0   27.50
2003-08-01  93        Boulder CC                     2003-09-01  0   70.00
2003-05-13  May03     Yellow Book USA                2003-06-12  56  285.00

Regards,
Americo Albuquerque


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



Current Thread
Keywords