Solved

XML file - regenerate/recreate after reorder

Posted on 2013-05-16
10
290 Views
Last Modified: 2013-05-16
I have a RSS feed, that I need to reorder for a client. I need to reorder the xml file, not the output(of course the output would be reordered as  the xml contents are reordered). So to be clear, it is not the output of the xml file that I am focused on, but the contents of the xml file. See examples below:

Here is my xml file:

<?xml-stylesheet type="text/xsl" href="tdome_trans2.xsl" ?>
<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, 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, 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, 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, 6:30 PM, Grarage</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
<item>
<title>Event 5</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 09:30 -0500</pubDate>
<location>June 05, 2013, 6:30 PM, Garage Bar</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
<item>
<title>Event 6</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 09:30 -0500</pubDate>
<location>June 20, 2013, 6:30 PM, The Old Mansion</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
</channel>
</rss>

Open in new window


Here is my xsl file that reorders based on the location
<?xml version="1.0"  encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml"/>
    <xsl:template match="*">
        <channel>
            <xsl:apply-templates select="channel"/>
        </channel>
    </xsl:template>

    <xsl:template match="channel">
        <xsl:apply-templates select="item">
            <xsl:sort select="location" order="ascending"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="item">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

Open in new window


here is the output via the browser:
Event 5 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 05, 2013, 6:30 PM, Garage Bar https://www.??????/?????.aspx Event 4 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 20, 2013, 6:30 PM, Grarage https://www.??????/?????.aspx Event 6 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 20, 2013, 6:30 PM, The Old Mansion https://www.??????/?????.aspx Event 3 https://www.??????/?????.aspx Wed, 15 June 2013 08:43 -0500 June 21, 2013, 7:00 PM, Auditoriam https://www.??????/?????.aspx Event 2 https://www.??????/?????.aspx Wed, 15 June 2013 08:43 -0500 June 23, 2013, 6:00 PM, Danny's Bar and Grill https://www.??????/?????.aspx Event 1 https://www.??????/?????.aspx Wed, 15 June 2013 08:46 -0500 June 29, 2013, 8:00 AM, Town Square https://www.??????/?????.aspx

Open in new window


The output above is now in the order i want, but I want the xml file with the nodes ordered correctly so I want to re-generate the xml file so it appears like this, notice that I have manually reordered the contents to show as an example: (forgive me for restating so much, but I am learning this to get a client request completed by Friday). Any assistance is appreciated!!

<?xml-stylesheet type="text/xsl" href="tdome_trans2.xsl" ?>
<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 5</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 09:30 -0500</pubDate>
<location>June 05, 2013, 6:30 PM, Garage Bar</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, 6:30 PM, Grarage</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
<item>
<title>Event 6</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 09:30 -0500</pubDate>
<location>June 20, 2013, 6:30 PM, The Old Mansion</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
<item>
<item>
<title>Event 3</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 08:43 -0500</pubDate>
<location>June 21, 2013, 7:00 PM, Auditoriam</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, 6:00 PM, Danny's Bar and Grill</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
<title>Event 1</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 08:46 -0500</pubDate>
<location>June 29, 2013, 8:00 AM, Town Square</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
</channel>
</rss>

Open in new window

0
Comment
Question by:lfwebz
  • 5
  • 5
10 Comments
 
LVL 18

Expert Comment

by:zc2
ID: 39172048
I've redesigned your XSLT, please take a look

<?xml version="1.0"  encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml"/>

    <xsl:template match="channel">
      <channel>
      	<xsl:apply-templates select="*[name() != 'item']"/>
        <xsl:apply-templates select="item">
            <xsl:sort select="location" order="ascending"/>
        </xsl:apply-templates>
      </channel>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
        	<xsl:apply-templates select="@*|*|text()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|text()">
        <xsl:copy/>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:lfwebz
ID: 39172127
Hi Zc2

Thank you for your input, but the problem is the output when I place tdome.xml in my browser is this:

Thunder Dome - Calendar - Villiage Ctrhttps://www.??????/?????.aspxWed, 15 June 2013 09:30 -0500Thunder Dome - Upcoming Eventsen-us Event 5 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 05, 2013, 6:30 PM, Garage Bar https://www.??????/?????.aspx Event 4 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 20, 2013, 6:30 PM, Grarage https://www.??????/?????.aspx Event 6 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 20, 2013, 6:30 PM, The Old Mansion https://www.??????/?????.aspx Event 3 https://www.??????/?????.aspx Wed, 15 June 2013 08:43 -0500 June 21, 2013, 7:00 PM, Auditoriam https://www.??????/?????.aspx Event 2 https://www.??????/?????.aspx Wed, 15 June 2013 08:43 -0500 June 23, 2013, 6:00 PM, Danny's Bar and Grill https://www.??????/?????.aspx Event 1 https://www.??????/?????.aspx Wed, 15 June 2013 08:46 -0500 June 29, 2013, 8:00 AM, Town Square https://www.??????/?????.aspx

Open in new window


but the client needs the modified xml file which would display similar to this:

<?xml version="1.0" ?>
<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 5</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 09:30 -0500</pubDate>
<location>June 05, 2013, 6:30 PM, Garage Bar</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, 6:30 PM, Grarage</location>
<guid>https://www.??????/?????.aspx</guid>
</item>
<item>
<title>Event 6</title>
<link>https://www.??????/?????.aspx</link>
<pubDate>Wed, 15 June 2013 09:30 -0500</pubDate>
<location>June 20, 2013, 6:30 PM, The Old Mansion</location>
<guid>https://www.??????/?????.aspx</guid>
</item>

Open in new window

0
 
LVL 18

Expert Comment

by:zc2
ID: 39172179
May be because the first line withe processing instruction is missed in the output?
Please try to replace the line 20 of my XSLT with the following:

    <xsl:template match="@*|text()|processing-instruction()">
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:lfwebz
ID: 39172296
There must be something I am doing wrong. the browser output is still:

Thunder Dome - Calendar - Villiage Ctrhttps://www.??????/?????.aspxWed, 15 June 2013 09:30 -0500Thunder Dome - Upcoming Eventsen-us Event 5 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 05, 2013, 6:30 PM, Garage Bar https://www.??????/?????.aspx Event 4 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 20, 2013, 6:30 PM, Grarage https://www.??????/?????.aspx Event 6 https://www.??????/?????.aspx Wed, 15 June 2013 09:30 -0500 June 20, 2013, 6:30 PM, The Old Mansion https://www.??????/?????.aspx Event 3 https://www.??????/?????.aspx Wed, 15 June 2013 08:43 -0500 June 21, 2013, 7:00 PM, Auditoriam https://www.??????/?????.aspx Event 2 https://www.??????/?????.aspx Wed, 15 June 2013 08:43 -0500 June 23, 2013, 6:00 PM, Danny's Bar and Grill https://www.??????/?????.aspx Event 1 https://www.??????/?????.aspx Wed, 15 June 2013 08:46 -0500 June 29, 2013, 8:00 AM, Town Square https://www.??????/?????.aspx

Open in new window


I made the change in line 20

<?xml version="1.0"  encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml"/>

    <xsl:template match="channel">
      <channel>
      	<xsl:apply-templates select="*[name() != 'item']"/>
        <xsl:apply-templates select="item">
            <xsl:sort select="location" order="ascending"/>
        </xsl:apply-templates>
      </channel>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
        	<xsl:apply-templates select="@*|*|text()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|text()|processing-instruction()">
        <xsl:copy/>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 18

Expert Comment

by:zc2
ID: 39172392
So, what does the browser show if you load the original xml into it?
0
 

Author Comment

by:lfwebz
ID: 39172414
if i remove this
<?xml-stylesheet type="text/xsl" href="tdome_trans4.xsl" ?>

Open in new window

from the original xml file and put in
<?xml version="1.0"  encoding="UTF-8"?>

Open in new window

, I see the xml broken out in it sections as nodes as I want to.  But the stylesheet is needed to pull in the xsl - is that correct.

I apologize for the ignorance on my part, I was literally handed this issue yesterday and the most i do with xml files is pull them in and display.  Never had to do anything like this before.
0
 
LVL 18

Accepted Solution

by:
zc2 earned 350 total points
ID: 39172621
I'm sorry, I don't have anything more to suggest. The input and output files are identical (except the different sorting) and all the browsers I tried show them both the same way.

You may want also change the line 3 of the XSLT to this:

<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>

It won't help, but since the input file did not have the XML declaration, so let's remove it from the output file as well.
0
 

Author Comment

by:lfwebz
ID: 39173110
Hi ZC2

Thanks so much for your help.  I went on and told the client it just couldn't be done and they needed to get their hosting provider to provide the correctly formatted xml.  i will try your suggestions, I am kind of curious now as to why this is not working.

Thanks again for all you imput.  I appreciate it.
0
 

Author Closing Comment

by:lfwebz
ID: 39173115
Was not able to resolve my specific issue, but really interesting, helpful feedback, particularly since I was learning at the same time as trying to get an unfamiliar project completed.   Thanks so much.
0
 
LVL 18

Expert Comment

by:zc2
ID: 39173122
You're welcome and I'm sorry I could not help fully resolve the issue
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel VBA and XML - traversing the nodes 2 77
Removing information on Duplicate XML Nodes 24 78
Word files to Wordpress Posts 2 53
MS SQL Database Generating XML using query 21 44
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question