Help sorting XML with XSL

Discussion in 'XML' started by dc24ua, Jun 15, 2006.

  1. dc24ua

    dc24ua Guest

    Hello -

    I need help sorting an xml file. I'd like to sort the xml based on the
    value found in <colvalue> of the second <col>.

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <?xml-stylesheet type="text/xsl" href="sort.xsl"?>
    <rows>
    <row>
    <col><colname>VIEW_ID</colname><colvalue><![CDATA[YZGOSDC63_Q002_V001_AV01]]></colvalue></col>
    <col><colname>VIEW_SIZE</colname><colvalue><![CDATA]></colvalue></col>
    <col><colname>TITLE</colname><colvalue><![CDATA[Small:
    Indicator]]></colvalue></col>
    <col><colname>PARENT_RPT</colname><colvalue><![CDATA[YZGOSDC63_Q002]]></colvalue></col>
    <col><colname>TEMPLATE_ID</colname><colvalue><![CDATA[ZGO_EART_WWW_TEMPLATE_001]]></colvalue></col>
    </row>
    <row>
    <col><colname>VIEW_ID</colname><colvalue><![CDATA[YZGOSDC63_Q002_V002_AV01]]></colvalue></col>
    <col><colname>VIEW_SIZE</colname><colvalue><![CDATA[A]]></colvalue></col>
    <col><colname>TITLE</colname><colvalue><![CDATA[Small: Latest
    Budget]]></colvalue></col>
    <col><colname>PARENT_RPT</colname><colvalue><![CDATA[YZGOSDC63_Q002]]></colvalue></col>
    <col><colname>TEMPLATE_ID</colname><colvalue><![CDATA[ZGO_EART_WWW_TEMPLATE_001]]></colvalue></col>
    </row>
    <row>
    <col><colname>VIEW_ID</colname><colvalue><![CDATA[YZGOSDC63_Q002_V003_AV01]]></colvalue></col>
    <col><colname>VIEW_SIZE</colname><colvalue><![CDATA[Z]]></colvalue></col>
    <col><colname>TITLE</colname><colvalue><![CDATA[Small: Last
    Year]]></colvalue></col>
    <col><colname>PARENT_RPT</colname><colvalue><![CDATA[YZGOSDC63_Q002]]></colvalue></col>
    <col><colname>TEMPLATE_ID</colname><colvalue><![CDATA[ZGO_EART_WWW_TEMPLATE_001]]></colvalue></col>
    </row>
    </rows>



    Here's the XSL I'm using. I've had some luck with <xsl:sort> , but I
    haven't gotten it to work the way I need it to.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <body>
    <table border="1">
    <xsl:for-each select="rows/row">
    <tr>
    <xsl:for-each select="col">
    <td><xsl:value-of select="colvalue"/></td>
    </xsl:for-each>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>
     
    dc24ua, Jun 15, 2006
    #1
    1. Advertising

  2. >I'd like to sort the xml based on the
    >value found in <colvalue> of the second <col>.


    <xsl:for-each select="rows/row">
    <xsl:sort select="col[2]/colvalue"/>
    ...

    .... right?

    BTW, in the example you showed us, those <![CDATA[]]> sections don't
    seem to be doing anything but burning bytes and wasting
    transmission/parse time. Do you really need them?

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 15, 2006
    #2
    1. Advertising

  3. dc24ua

    dc24ua Guest

    That did the trick. Thanks!
    You are correct that the CDATA isn't needed in this example. However, I
    do have some situations where the data contained in <colvalue> is
    html/xml . Perhaps I should search the string on the server side and
    only add CDATA when needed. Thanks pointing it out.

    Cheers


    Joe Kesselman wrote:
    > >I'd like to sort the xml based on the
    > >value found in <colvalue> of the second <col>.

    >
    > <xsl:for-each select="rows/row">
    > <xsl:sort select="col[2]/colvalue"/>
    > ...
    >
    > ... right?
    >
    > BTW, in the example you showed us, those <![CDATA[]]> sections don't
    > seem to be doing anything but burning bytes and wasting
    > transmission/parse time. Do you really need them?
    >
    > --
    > () ASCII Ribbon Campaign | Joe Kesselman
    > /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    dc24ua, Jun 15, 2006
    #3
  4. dc24ua wrote:
    > do have some situations where the data contained in <colvalue> is
    > html/xml .


    Simply wrapping it in <![CDATA[]]> will not always be enough in that
    case. (Consider what happens when the contained XML itself contains a
    CDATA section.)

    If you're building the document through standard XML tools, they can
    take care of escaping characters for you. If you're building it by doing
    string manipulation, you're buying yourself a bunch of hassles.

    Actually, the best answer is usually to make the contained XML just be
    ordinary XML structure rather than trying to store its string
    representation -- among other things, that way you don't have to
    re-parse it if you need to look at it. Contained HTML... Well, I'd make
    it XHTML and refer you to the preceeding sentence, but to each their own.


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 15, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim
    Replies:
    5
    Views:
    7,032
    Thomas 'PointedEars' Lahn
    Mar 2, 2004
  2. Sparko
    Replies:
    0
    Views:
    421
    Sparko
    Jul 9, 2004
  3. Replies:
    1
    Views:
    3,688
    A. Bolmarcich
    May 27, 2005
  4. Replies:
    3
    Views:
    1,318
    George Bina
    Sep 19, 2007
  5. Ben Fidge

    Sorting XML using XMLDOM without using XSL

    Ben Fidge, Dec 9, 2005, in forum: ASP General
    Replies:
    0
    Views:
    121
    Ben Fidge
    Dec 9, 2005
Loading...

Share This Page