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 Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT just sort the XML 1

Status
Not open for further replies.

jstreich

Programmer
Apr 20, 2002
1,067
US
I have a atom file file that looks like
Code:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="[URL unfurl="true"]http://www.w3.org/2005/Atom">[/URL]
  <title>Zimbra CIE_Events</title>
  <generator>Zimbra Atom Feed Servlet</generator>
  <updated>2009-04-16T15:41:55-05:00</updated>
  <entry>
    <title>Culture Cafe </title>
    <updated>2009-04-23T14:00:00-05:00</updated>
    <summary/>
    <author>
      <name/>
      <email>foo@uwm.edu</email>
    </author>
  </entry>
...
</feed>

I want/need it in the same format, but I need it sorted.

I tried:
Code:
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">

  <xsl:output method="xml"/>

  <xsl:template match="/feed/entry">
    <xsl:apply-templates>
      <xsl:sort select="updated"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

The result isn't pretty, but it's not terribly far from what I want. What I get back is:
Code:
Zimbra CIE_EventsZimbra Atom Feed Servlet[URL unfurl="true"]http://login01.pantherlink.uwm.edu/service/home/pooney/CIE_Events.atom2009-04-16T15:59:46-05:00Culture[/URL] Cafe 2009-04-23T14:00:00-05:00foo@uwm.edu...

I've not toched XSLT in a long time, and I'm looking for a fast and cheap solution.
 
[tt]<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[ignore][/ignore]"
xmlns="[ignore][/ignore]"
xmlns:atom="[ignore][/ignore]"
>
<xsl:eek:utput method="xml" version="1.0" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()" />
</xsl:copy>
</xsl:template>
<xsl:template match="atom:feed">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="atom:updated" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>[/tt]
 
Thanks, that gets me valid xml, but the sort doesn't appear to work; what I get out is no different than what went in. :(
 
It should sort (?) to my understanding. If the original feed is already in some order, it could result in the same output. Try ascending and then descending setting for order attribute, in that case it should show up different result for sure one way or another.
[tt] <xsl:sort select="atom:updated" order="ascending" /> <!-- or "descending" -->
[/tt]
 
I have just checked against a randomly chosen atom feed out-there:
ref Their entries are in the order of "descending". If I use the xsl as I posted, supplemented with additional namespaces proper to that feed, I can obtain output reversing the order.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]"
xmlns:eek:penSearch="[ignore][/ignore]"
xmlns:georss="[ignore][/ignore]"
xmlns:gd="[ignore][/ignore]"
xmlns="[ignore][/ignore]"
xmlns:atom="[ignore][/ignore]"
>
<xsl:eek:utput method="xml" version="1.0" encoding="utf-8" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()" />
</xsl:copy>
</xsl:template>
<xsl:template match="atom:feed">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="atom:updated" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top