Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 13th, 2003, 12:18 PM
Registered User
 
Join Date: Aug 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl:sort- select attribute, syntax question

Hello,

  I am working on an xslt doc that has need for sorting, but can't quite get my syntax right.

here is the relevent input xml code:

<ReportData>
  <DataRow rowNumber="2">
    <ColData colID="2" value="2003-07-01" />
    <ColData colID="3" value="70103" />
    <ColData colID="4" value="AMS Courier" />
    <ColData colID="5" value="2003-08-15" />
    <ColData colID="7" value="27.50" />
  </DataRow>
  <DataRow rowNumber="3">
    <ColData colID="2" value="2003-07-03" />
    <ColData colID="4" value="AMS Courier" />
    <ColData colID="5" value="2003-08-15" />
    <ColData colID="7" value="20.00" />
  </DataRow>
</ReportData>

here is the relevent xslt code: (doesn't work)

<xsl:for-each select="ReportData/DataRow">
<xsl:sort select="ColData/@value[ColData/@colID='4']"
          data-type="text"
          order="descending" />

soooo...

what I want is to sort all DataRows alphabetically based on the ColData/@value, where that element has @colID = '4'

xml/xslt is such a pain in the a**

thank yew all
 
Old August 13th, 2003, 02:52 PM
Registered User
 
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<xsl:for-each select="//DataRow/ColData[@colID='4']">
        <xsl:sort select="@value" data-type="text" order="descending"/>
        <xsl:value-of select="@value"/>
</xsl:for-each>

 
Old August 13th, 2003, 04:02 PM
Registered User
 
Join Date: Aug 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the post,

  However my problem remains, - 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).

  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']">
      <xsl:sort select="@value" data-type="text" />
    <tr>
      <td>
        <xsl:value-of select="@value"/>
      </td>
    </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Here is new input XML test code: *************************************

<?xml version="1.0"?>
<ReportRet>
  <ReportData>
    <DataRow rowNumber="2">
      <ColData colID="2" value="2003-07-01" />
      <ColData colID="3" value="70103" />
      <ColData colID="4" value="AMS Courier" />
      <ColData colID="5" value="2003-08-15" />
      <ColData colID="6" value="0" />
      <ColData colID="7" value="27.50" />
    </DataRow>
    <DataRow rowNumber="2">
      <ColData colID="2" value="2003-05-13" />
      <ColData colID="3" value="May03" />
      <ColData colID="4" value="Yellow Book USA" />
      <ColData colID="5" value="2003-06-12" />
      <ColData colID="6" value="56" />
      <ColData colID="7" value="285.00" />
    </DataRow>
    <DataRow rowNumber="3">
      <ColData colID="2" value="2003-07-23" />
      <ColData colID="3" value="29190087" />
      <ColData colID="4" value="Aegis Staffing Services, Inc." />
      <ColData colID="5" value="2003-08-22" />
      <ColData colID="6" value="0" />
      <ColData colID="7" value="260.00" />
    </DataRow>
    <DataRow rowNumber="4">
      <ColData colID="2" value="2003-08-01" />
      <ColData colID="3" value="93" />
      <ColData colID="4" value="Boulder CC" />
      <ColData colID="5" value="2003-09-01" />
      <ColData colID="6" value="0" />
      <ColData colID="7" value="70.00" />
    </DataRow>
  </ReportData>
</ReportRet>

Above gives me results as below:
(just for the one ColData element (colID='4')
************************************************** *******************

Aegis Staffing Services, Inc.
AMS Courier
Boulder CC
Yellow Book USA


tanx all

 
Old August 14th, 2003, 12:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

try this stylesheet:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="ReportData">
        <html>
            <head>
                <title>...</title>
            </head>
            <body>
                <table border="1">
                    <xsl:apply-templates>
                        <xsl:sort select="ColData/@value[../@colID = 4]"/>
                    </xsl:apply-templates>
                </table>    
            </body>
        </html>
    </xsl:template>

    <xsl:template match="DataRow">
        <tr>
            <xsl:apply-templates/>
        </tr>
    </xsl:template>

    <xsl:template match="ColData">
        <td>
            <xsl:value-of select="@value"/>
        </td>
    </xsl:template>
</xsl:stylesheet>
Regards,
Armen





Similar Threads
Thread Thread Starter Forum Replies Last Post
sort on specific attribute kgoldvas XSLT 2 July 17th, 2007 09:42 AM
<xsl:for-each> with a variable select="attribute" BeneathClouds XSLT 1 August 1st, 2005 03:36 AM
xsl:sort Question kwilliams XSLT 6 July 18th, 2005 08:39 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
Noob needs help with XSL syntax question lancia12 XSLT 1 October 1st, 2004 08:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.