问题
I am building an RSS parser that takes in the media namespace's items.
Example 1:
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Some channel</title>
<item>
<guid>234wwerwe</guid>
<title>Some title</title>
<media:description>test description 1</media:description>
<pubDate>Tue, 30 Jul 2019 19:24:00 +0000</pubDate>
</item>
</channel>
</rss>
Example 2:
<rss version="2.0" xmlns:media="http://www.rssboard.org/media-rss">
<channel>
<title>Some second channel</title>
<item>
<guid>234wwsdflkjl23we</guid>
<title>Some other title</title>
<media:description>test description 2</media:description>
<pubDate>Tue, 30 Jul 2019 19:24:00 +0000</pubDate>
</item>
</channel>
</rss>
I would like to convert this using the same xsl file but if I do something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:boardMedia="http://www.rssboard.org/media-rss">
<xsl:template match="/">
<xsl:for-each select="rss/channel/item">
<item>
<xsl:element name="referenceId">
<xsl:value-of select="guid" />
</xsl:element>
<xsl:element name="title">
<xsl:value-of select="title" />
</xsl:element>
<xsl:element name="description">
<xsl:value-of select="media:description" />
<xsl:value-of select="boardMedia:description" />
</xsl:element>
<xsl:element name="itemPublishDate"><xsl:value-of select="pubDate" /></xsl:element>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
then it only successfully pulls the description for the first rss, not the second.
Any suggestions on how to do this in one xsl file?
回答1:
As I mentioned in a comment, the problem cannot be reproduced using your code.
Note that you could simplify your code significantly:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/rss">
<xsl:for-each select="channel/item">
<item>
<referenceId>
<xsl:value-of select="guid" />
</referenceId>
<title>
<xsl:value-of select="title" />
</title>
<description>
<xsl:value-of select="*:description" />
</description>
<itemPublishDate>
<xsl:value-of select="pubDate" />
</itemPublishDate>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/bFN1yan/1
回答2:
If you really want to merge these two description
elements with your XSLT, you can try the following stylesheet (naming your second XML file example_2.xml
):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"
xmlns:boardMedia="http://www.rssboard.org/media-rss">
<xsl:variable name="file2" select="document('example_2.xml')" /> <!-- Added to include the second file -->
<xsl:template match="/">
<xsl:for-each select="rss/channel/item">
<item>
<xsl:element name="referenceId">
<xsl:value-of select="guid" />
</xsl:element>
<xsl:element name="title">
<xsl:value-of select="title" />
</xsl:element>
<xsl:element name="description">
<xsl:value-of select="media:description" />
<xsl:value-of select="$file2/rss/channel/item/boardMedia:description" />
</xsl:element>
<xsl:element name="itemPublishDate"><xsl:value-of select="pubDate" /></xsl:element>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Its result is rather odd:
<?xml version="1.0" encoding="UTF-8"?>
<item xmlns:media="http://search.yahoo.com/mrss/" xmlns:boardMedia="http://www.rssboard.org/media-rss">
<referenceId>234wwerwe</referenceId>
<title>Some title</title>
<description>test description 1test description 2</description>
<itemPublishDate>Tue, 30 Jul 2019 19:24:00 +0000</itemPublishDate>
</item>
But that seems to be the result you like.
来源:https://stackoverflow.com/questions/57616948/how-to-have-the-same-xslt-namespace-with-different-uris