[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Selecting similar XML nodes by a sort criteria

Subject: Selecting similar XML nodes by a sort criteria
From: "Chaudhary, Harsh" <HCHAUDHA@xxxxxxxxx>
Date: Thu, 4 Oct 2007 12:41:01 -0500
 Selecting similar XML nodes by a sort criteria
Hi,

I have the following XML. I would like to write XSLT which outputs the
above XML in an HTML table. This table should be sorted by 'areacode'.

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
    <zipcode>
        <book areacode='1'>
            <title lang="eng">Harry Potter</title>
            <price>29.99</price>
        </book>
        <book areacode='2'>
            <title lang="eng">Learning XML</title>
            <price>39.95</price>
        </book>
    </zipcode>
    <zipcode>
        <book areacode='2'>
            <title lang="eng">Count of Monte Cristo</title>
            <price>29.99</price>
        </book>
        <book areacode='3'>
            <title lang="eng">Tom Sawyer</title>
            <price>39.95</price>
        </book>
    </zipcode>
</bookstore>

This part is fine. The problem comes when I try to color the rows in the
table so that the rows with the same areacode are the same color and the
colors alternate for rows that are not the same area code e.g. I have 2
row colors, gray and blue. This is what the HTML should look like(sorry
about the poor HTML markup, this is just for illustration):

<html>
    <body>
        <table>
            <tr>
                <td>Area Code</td>
                <td>Title</td>
            </tr>
            <tr bgcolor='gray'>
                <td>1</td>
                <td>Harry Potter</td>
            </tr>
            <tr bgcolor='blue'>
                <td>2</td>
                <td>Learning XML</td>
            </tr>
            <tr bgcolor='blue'>
                <td>2</td>
                <td>Count of Monte Cristo</td>
            </tr>
            <tr bgcolor='gray'>
                <td>3</td>
                <td>Tom Sawyer</td>
            </tr>
        </table>
    </body>
</html>

What I tried was after doing something like (pseudocode):

for-each(areacode)
sort(by areacode)
-----
write out the html row

I need some logic or pseudocode which I can insert in the line marked
(-----) so that it would selectively apply the style attribute with the
appropriate color with each row.

I am hoping that my question is clear enough. But I could also write the
full XSLT if that would help. I made up this example, so I don't have
the XSLT code for this particular example.

Does anyone know of any XSLT or XPath function I could use.

I am trying to use the XPath axes preceding-sibling. But its not working
for me.

Thanks,
HC.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.