Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort XML entries

Status
Not open for further replies.

csbdeady

Programmer
May 18, 2002
119
GB
Hi

I have an XML file, for simplicity we'll say:

<?xml version="1.0" encoding="ISO-8859-1"?>
<entry id="0">
<date_start>01/01/99</date_start>
<date_end>01/01/99</date_end>
<sometext>hello world</some text>
</entry>
<entry id="1">
<date_start>01/01/00</date_start>
<date_end>01/01/00</date_end>
<sometext>hello other world</some text>
</entry>


If I parse the above as-is using:

$html = xslt_process($parser, 'myxmlfile.xml', 'myxslfile.xsl');
echo $html;

I will get the events in ascending order, ie: oldest first.

However I need to display them in descending order.

I have two options - firstly reorder the entries within the xml file itself (which I would rather not do), or secondly leave the file as-is but process the xml in such a way that event 1 is displayed before event 0.

Please can anyone help me to achieve the latter.

Many thanks
-Colin
 
Hi again

I should have said that I'm using:

<xsl:template match="/">
<table border="0">
<xsl:apply-templates>
<xsl:sort select="title" order="descending" data-type="text" />
</xsl:apply-templates>
</table>
</xsl:template>

but regardless or whether the order is ascending or descending, the output is always in ascending order.
 
Why does the xsl:sort not do it ?, is it realted to your other post ?
 
I am not sure why xsl:sort is not sorting the output of my xml at all. Yes the two posts are related.

I'm currently stuck with on this one - all the documents I read suggest that xsl:sort as used in my XSLT file should transform the document to be in descending order by the title element, but unfortunately I get ascending order still.

Most appreciative in advance of any help you can give on this one!
 
Ok fixed it;)

For reference in case someone else needs the assistance and finds this thread, the xsl:sort wasn't working when I had:

<xsl:template match="/">
<table border="0">
<xsl:apply-templates>
<xsl:sort order="ascending" select="title" />
</xsl:apply-templates>
</table>
</xsl:template>

but is when I change to:

<xsl:template match="events">
<table border="0">
<xsl:apply-templates>
<xsl:sort order="ascending" select="title" />
</xsl:apply-templates>
</table>
</xsl:template>

My tree structure is:

<eventlisting>
<events>
<event>
...
</event>
</events>
<eventlisting>

The sort was attempting to be undertaken against the root <eventlisting> level but should have been one down, ie: sort all the events by event title, and not try to sort the single eventlisting;)

-Colin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top