如何使用具有不同 URI 的相同 XSLT 命名空间? [英] How to have the same XSLT namespace with different URIs?

查看:58
本文介绍了如何使用具有不同 URI 的相同 XSLT 命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个接收媒体命名空间项目的 RSS 解析器.

I am building an RSS parser that takes in the media namespace's items.

示例 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>

示例 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>

我想使用相同的 xsl 文件进行转换,但如果我执行以下操作:

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>

然后它只成功提取第一个 rss 的描述,而不是第二个.

then it only successfully pulls the description for the first rss, not the second.

有关如何在一个 xsl 文件中执行此操作的任何建议?

Any suggestions on how to do this in one xsl file?

推荐答案

正如我在评论中提到的,使用您的代码无法重现该问题.

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>

<小时>

演示:https://xsltfiddle.liberty-development.net/bFN1yan/1

这篇关于如何使用具有不同 URI 的相同 XSLT 命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆