Solved

XML file - regenerate/recreate after reorder

Posted on 2013-05-16
10
284 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now