Replacing a Namespace with XSLT

Posted by er4z0r on Stack Overflow See other posts from Stack Overflow or by er4z0r
Published on 2010-03-21T11:03:33Z Indexed on 2010/03/21 11:11 UTC
Read the original article Hit count: 462

Filed under:
|
|

Hi I want to work around a 'bug' in certain RSS-feeds, which use an incorrect namespace for the mediaRSS module. I tried to do it by manipulating the DOM programmatically, but using XSLT seems more flexible to me.

Example:

<media:thumbnail xmlns:media="http://search.yahoo.com/mrss" url="http://www.suedkurier.de/storage/pic/dpa/infoline/brennpunkte/4311018_0_merkelxI_24280028_original.large-4-3-800-199-0-3131-2202.jpg" />
<media:thumbnail url="http://www.suedkurier.de/storage/pic/dpa/infoline/brennpunkte/4311018_0_merkelxI_24280028_original.large-4-3-800-199-0-3131-2202.jpg" />

Where the namespace must be http://search.yahoo.com/mrss/ (mind the slash).

This is my stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="//*[namespace-uri()='http://search.yahoo.com/mrss']">
        <xsl:element name="{local-name()}" namespace="http://search.yahoo.com/mrss/" >
            <xsl:apply-templates select="@*|*|text()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Unfortunately the result of the transformation is an invalid XML and my RSS-Parser (ROME Library) does not parse the feed anymore:

java.lang.IllegalStateException: Root element not set
    at org.jdom.Document.getRootElement(Document.java:218)
    at com.sun.syndication.io.impl.RSS090Parser.isMyType(RSS090Parser.java:58)
    at com.sun.syndication.io.impl.FeedParsers.getParserFor(FeedParsers.java:72)
    at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:273)
    at com.sun.syndication.io.WireFeedInput.build(WireFeedInput.java:251)
    ... 8 more

What is wrong with my stylesheet?

© Stack Overflow or respective owner

Related posts about rss

Related posts about Xml