Solved

XML file - regenerate/recreate after reorder

Posted on 2013-05-16
10
287 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

19 Experts available now in Live!

Get 1:1 Help Now