Help sorting XML with XSL

D

dc24ua

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>
 
J

Joe Kesselman

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?
 
D

dc24ua

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 said:
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?
 
J

Joe Kesselman

dc24ua said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top