user1176783
user1176783

Reputation: 673

xml parsing and reversing order

Granted I am just learning to do this, but I think I'm making it harder than it needs to be

We, well, I have been given this xml file. it's just made up of a bunch of events data, but the group that needs this data needs the data reversed. Currently, it not displaying most recent dates first.

What is the best method to parse through this file, reverse the order and then spit it back out in xml?

Any help would be appreciated.

Example of xml below: I want to reorder/reverse order the location field/node for each of the Events [1-4]

<rss version="2.0">
    <channel>
        <title>Thunder Dome - Calendar - Villiage Ctr</title>
        <link>https://www.??????/?????.aspx</link>
        <update>Wed, 15 June 2013 09:30 -0500</update>
        <location>Thunder Dome - Upcoming Events</location>
        <language>en-us</language>
        <item>
            <title>Event 1</title>
            <link>https://www.??????/?????.aspx</link>
            <pubDate>Wed, 15 June 2013 08:46 -0500</pubDate>
            <location>June 29, 2013<br>, 8:00 AM, Town Square</location>
            <guid>https://www.??????/?????.aspx</guid>
        </item>
        <item>
            <title>Event 2</title>
            <link>https://www.??????/?????.aspx</link>
            <pubDate>Wed, 15 June 2013 08:43 -0500</pubDate>
            <location>June 23, 2013<br>, 6:00 PM, Danny's Bar and Grill</location>
            <guid>https://www.??????/?????.aspx</guid>
        </item>
        <item>
        <title>Event 3</title>
            <link>https://www.??????/?????.aspx</link>
            <pubDate>Wed, 15 June 2013 08:43 -0500</pubDate>
            <location>June 21, 2013<br>, 7:00 PM, Auditoriam</location>
            <guid>https://www.??????/?????.aspx</guid>
        </item>
        <item>
            <title>Event 4</title>
            <link>https://www.??????/?????.aspx</link>
            <pubDate>Wed, 15 June 2013 09:30 -0500</pubDate>
            <location>June 20, 2013<br>, 6:30 PM, Grarage</location>
            <guid>https://www.??????/?????.aspx</guid>
        </item>
    </channel>
</rss>

Upvotes: 0

Views: 1440

Answers (2)

davidfmatheson
davidfmatheson

Reputation: 3567

If you know how to use XSLT, here is a previous article doing what you're describing:

XSLT: How to reverse output without sorting by content

Your posted XML is not valid, first off, so you would have to surround your <location> elements with CDATA sections if you wanted to parse it, like so:

<location><![CDATA[June 29, 2013<br>, 8:00 AM, Town Square]]></location>

If it is just the <location> node that you want to reverse the order of the date and place, such that the output looks like:

<location>Town Square, June 29, 2013<br>, 8:00 AM</location>

You could get away without parsing XML, just use a regular expression to swap the order for that particular node:

sed 's/<location>\(.* [AP]M\), \(.*\)</<location>\2, \1</g' rss.xml

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163262

It's very easy to do this in XSLT. Though learning a new language for the task might be a bit of an overhead, the skills will come in handy with any subsequent XML work.

You basically need two template rules. The first copies everything unchanged:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

and the second handles the reordering of items in the channel element:

<xsl:template match="channel">
  <xsl:apply-templates select="*[not(self::item)]"/>
  <xsl:apply-templates select="item">
    <xsl:sort select="-position()" data-type="number"/>
  </xsl:apply-templates>
</xsl:template>

Upvotes: 1

Related Questions