p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Sort Table Columns by Table Header Attribute (http://p2p.wrox.com/showthread.php?t=43283)

omrieliav May 31st, 2006 10:25 AM

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>


mhkay June 4th, 2006 03:53 PM

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

omrieliav June 5th, 2006 12:50 AM

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


mhkay June 5th, 2006 02:32 AM

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

omrieliav June 7th, 2006 01:05 AM

It's working!
Great.
Thanks much Michael



All times are GMT -4. The time now is 06:53 PM.

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