Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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>

Reply With Quote
  #2 (permalink)  
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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #3 (permalink)  
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

Reply With Quote
  #4 (permalink)  
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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #5 (permalink)  
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.