Xml 用XSLT替换名称空间
Hi I want to,它为mediaRSS模块使用了不正确的命名空间。我试图通过编程方式操纵DOM来实现这一点,但对我来说,使用XSLT似乎更灵活 例如:Xml 用XSLT替换名称空间,xml,xslt,rss,Xml,Xslt,Rss,Hi I want to,它为mediaRSS模块使用了不正确的命名空间。我试图通过编程方式操纵DOM来实现这一点,但对我来说,使用XSLT似乎更灵活 例如: <media:thumbnail xmlns:media="http://search.yahoo.com/mrss" url="http://www.suedkurier.de/storage/pic/dpa/infoline/brennpunkte/4311018_0_merkelxI_24280028_original.lar
<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" />
不幸的是,转换的结果是一个无效的XML,并且我的RSS解析器()不再解析提要:
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
我的样式表有什么问题?您的样式表中有一半的解决方案 您已经输入了一个模板来匹配(并更正)具有错误名称空间的元素,但是您没有任何东西来匹配RSS提要中的其他元素/属性 将匹配其余的文档节点,这将只将文本节点复制到输出中。这不会保留原始RSS提要的XML,并生成无效RSS XML结构的输出
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--identity transform that will copy matched node/attribute to the output and apply templates for it's children and attached attributes-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()" />
</xsl:copy>
</xsl:template>
<!--Specialized template to match on elements with the incorrect namespace and generate a new element-->
<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>
添加模板将确保将其他节点和属性复制到输出中,并保留文档内容/结构
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--identity transform that will copy matched node/attribute to the output and apply templates for it's children and attached attributes-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()" />
</xsl:copy>
</xsl:template>
<!--Specialized template to match on elements with the incorrect namespace and generate a new element-->
<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>
OK。我肯定需要在XSL/XPATH等方面投入更多的时间!