Search code examples
phpxmlxsltrssatom-feed

Restructure XML File using XSLT or PHP script


I have an iphone app which parses standard rss feeds and displays it on a table. However I have been given a feed from a client but the parser in the app cannot pickup the nodes and parse the data as it is not a standard rss feed.

The layout they have given me is the following:

    <rss version="0.92">
        <channel>
        <title>Feed</title>
        <link>http://google.com</link>
        <description>
        Description here
        </description>
        <lastBuildDate>30 June +0100</lastBuildDate>
        <language>en</language>
            <event>
                <eventID>123</eventID>
                <name>Name here</name>
                <date>2012-06-29</date>
                <time>21:00</time>
                <category>Arts</category>
                <info>
                 Info here
                </info>
            </event>
            <event>
                <eventID>223</eventID>
                <name>Name here</name>
                <date>2012-06-30</date>
                <time>22:00</time>
                <category>Dance</category>
                <info>
                 Info here
                </info>
            </event>
    </channel>
</rss>

Is there any way to restructure this xml file to a standard rss feed layout using XSLT or a PHP script? A standard rss feed layout being the following:

<rss>
     <channel>
     <item>
        <title>
            <![CDATA[ Title here ]]>
        </title>
        <link>
            http://www.theatre.com/
        </link>
        <guid>
        http://www.theatre.com
        </guid>
        <description> 
         <p> Description </p>

        </description>
        <dc:subject>
        <![CDATA[ ]]>
        </dc:subject>
        <dc:date>2013-02-01T18:00:04+00:00</dc:date>
      </item>
     </channel>
</rss>

Solution

  • It's not clear what you want, so this style-sheet is just indicative of what you might need.

    This XSLT 1.0 style-sheet ....

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/">
     <rss>
      <channel>
        <xsl:apply-templates select="rss/channel/event" /> 
      </channel>
     </rss>
    </xsl:template>
    
    <xsl:template match="event" >
     <item>
      <title><xsl:value-of select="../title" /></title>
      <link><xsl:value-of select="../link" /></link>
      <guid>http://www.theatre.com</guid>
      <description><xsl:value-of select="../description" /></description>
      <subject><xsl:value-of select="category" /></subject>
      <date><xsl:value-of select="date" />T<xsl:value-of select="time" />+00:00</date>
     </item>
    </xsl:template>      
    
    </xsl:stylesheet>
    

    ... when applied to your supplied sample input will produce this document ...*

    <?xml version="1.0" encoding="utf-8"?>
    <rss>
      <channel>
        <item>
          <title>Feed</title>
          <link>http://google.com</link>
          <guid>http://www.theatre.com</guid>
          <description>
            Description here
            </description>
          <subject>Arts</subject>
          <date>2012-06-29T21:00+00:00</date>
        </item>
        <item>
          <title>Feed</title>
          <link>http://google.com</link>
          <guid>http://www.theatre.com</guid>
          <description>
            Description here
            </description>
          <subject>Dance</subject>
          <date>2012-06-30T22:00+00:00</date>
        </item>
      </channel>
    </rss>