470,810 Members | 885 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,810 developers. It's quick & easy.

Help sorting XML with XSL

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[S]]></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>

Jun 14 '06 #1
3 1082
>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
Jun 15 '06 #2
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


Jun 15 '06 #3
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
Jun 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by dont bother | last post: by
2 posts views Thread by D. Roshani | last post: by
3 posts views Thread by Don | last post: by
2 posts views Thread by rookiejavadude | last post: by
5 posts views Thread by jrod11 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.