Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Help sorting XML with XSL

Reply
Thread Tools

Help sorting XML with XSL

 
 
dc24ua
Guest
Posts: n/a
 
      06-14-2006
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"
xmlnssl="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>

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      06-15-2006
>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
 
Reply With Quote
 
 
 
 
dc24ua
Guest
Posts: n/a
 
      06-15-2006
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


 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      06-15-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP/XML/XSL Sorting Problem westguard@gmail.com XML 3 09-19-2007 10:05 AM
Sorting XML using XMLDOM without using XSL Ben Fidge ASP General 0 12-09-2005 10:41 AM
XSL Question tp xsl:for-each and xsl:variable schaf@2wire.ch XML 1 05-27-2005 09:25 PM
sorting <xsl:for-each> versus <xsl:apply-templates> Sparko XML 0 07-09-2004 09:03 AM
XML XSL javascript, sorting child node of data island Tim XML 5 03-02-2004 07:42 PM



Advertisments