Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 May 31st, 2006, 10:25 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sort Table Columns by Table Header Attribute

Hi,

Got this XML that should be transformed to HTML table.
Can I sort the columns according to the "index" attribute of the <col> tag

Thanks Much

<table>
    <col text="My Col 1" index="2" />
    <col text="My Col 2" index="4" />
    <col text="My Col 3" index="1" />
    <col text="My Col 4" index="3" />
    <tr>
        <td>My Cell 1</td>
        <td>My Cell 2</td>
        <td>My Cell 3</td>
    </tr>
    <tr>
        <td>My Cell 10</td>
        <td>My Cell 20</td>
        <td>My Cell 30</td>
    </tr>
    <tr>
        <td>My Cell 100</td>
        <td>My Cell 200</td>
        <td>My Cell 300</td>
    </tr>
</table>

 
Old June 4th, 2006, 03:53 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Why are there four <col> elements when there only appear to be three columns in your table? I don't think I've understood the problem correctly.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 5th, 2006, 12:50 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry. I put extra "col" in my XML example. It should be like that:

<table>
    <col text="My Col 1" index="2" />
    <col text="My Col 2" index="3" />
    <col text="My Col 3" index="1" />
    <tr>
        <td>My Cell 1</td>
        <td>My Cell 2</td>
        <td>My Cell 3</td>
    </tr>
    <tr>
        <td>My Cell 10</td>
        <td>My Cell 20</td>
        <td>My Cell 30</td>
    </tr>
    <tr>
        <td>My Cell 100</td>
        <td>My Cell 200</td>
        <td>My Cell 300</td>
    </tr>
</table>


The result should be the following HTML:

<table>
    <tr>
        <td>My Cell 3</td>
        <td>My Cell 1</td>
        <td>My Cell 2</td>
    </tr>
    <tr>
        <td>My Cell 30</td>
        <td>My Cell 10</td>
        <td>My Cell 20</td>
    </tr>
    <tr>
        <td>My Cell 300</td>
        <td>My Cell 100</td>
        <td>My Cell 200</td>
    </tr>
</table>

My problem is sorting the "td"s based on attributes I don't have in current node (col/@index).
The <xsl:sort> command should be put in the "td"s <xsl:for-each> or <xsl:apply-templates> I guess. But how do I point the sort select to the right "col" "index" attribute?
<xsl:sort select="col[...???...]/@index" /> ?

Thanks a lot

 
Old June 5th, 2006, 02:32 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try:

<xsl:sort select="../../col[position() = count(current()/preceding-sibling::*)+1]/@index" data-type="number"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 7th, 2006, 01:05 AM
Registered User
 
Join Date: May 2006
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's working!
Great.
Thanks much Michael





Similar Threads
Thread Thread Starter Forum Replies Last Post
Fixing header, and first column of table. rupen CSS Cascading Style Sheets 5 October 21st, 2005 08:33 AM
problem with scrollbar and a table header touyets HTML Code Clinic 0 August 25th, 2005 06:45 AM
Sort By when table dynamically built using FSO nancy Classic ASP Databases 6 September 11th, 2004 04:17 AM
Table Sort With Javascript Ben Horne Javascript How-To 5 November 6th, 2003 05:50 AM





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