xslt没有从命名空间元素获取值

时间:2013-04-23 18:50:37

标签: xml xslt xml-namespaces

我正在尝试从名称空间为dc的元素中检索数据。除了这些值之外,其他一切都正常工作。

以下是我的XML的缩写版本:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet href="sci.xsl" type="text/xsl" ?>

<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

<channel>
    <title>SciTimes News</title>
    <link>home.htm</link>

    <description>SciTimes delivers up-to-the-minute news and information on the latest stories from the world of science and technology.</description>

    <dc:language>en-us</dc:language>

    <dc:date>2008-03-24T12:22:54+09:00</dc:date>

    <sy:updatePeriod>hourly</sy:updatePeriod>

    <sy:updateFrequency>1</sy:updateFrequency>

    <sy:updateBase>2000-01-01T12:00+00:00</sy:updateBase>


    <image>
        <title>SciTimes.com</title>
        <link>home.htm</link>
        <url>scitimes.jpg</url>
        <width>620</width>
        <height>96</height>
        <description>SciTimes delivers up-to-the-minute news and information on the latest stories from the world of  science and technology.</description>
    </image>

    <item>
        <title>Visual Memory</title>
        <link>vm.htm</link>
        <description>BOULDER, Colorado - The ability to retain memory about the details of a natural scene is unaffected by the distraction of another activity and this information is retained in "working memory" according to researchers at the University of Colorado School of Medicine. These results reinforce the notion that humans maintain useful information about previous fixations in long-term working memory rather than the limited capacity of visual short-term memory (VSTM).</description>
        <dc:pubDate>Mon, 24 Mar 2008 12:17:37 EST</dc:pubDate>
        <dc:subject>Biology</dc:subject>
    </item>
</channel>
</rss>

这是我的XSL:

<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://purl.org/dc/elements/1.1"
version="1.0"
>
<!-- Start base template -->
<xsl:template match="/">
    <html>
        <head>
            <title><xsl:value-of select="rss/channel/title" /></title>
            <link rel="stylesheet" href="sci.css" />
        </head>
        <body>
                <div id="logo">
                <xsl:apply-templates select="//image"/>
            </div>
            <div id="datetime"><xsl:value-of select="//dc:date" /></div>
            <div id="links">
                <p><xsl:value-of select="//description" /></p>
                <p><img src="links.jpg" /></p>
            </div>
            <div id="news">
                <h1>RSS News Feed</h1>
                <dc:stylesheet>
                <xsl:apply-templates select="//item" /></dc:stylesheet>
            </div>
        </body>
    </html>
</xsl:template>
<!-- End base template -->

<!-- Start logo template -->
<xsl:template match="//image">
    <a href="{link}"><img src="{url}" alt="{title}" width="{width}" height="{height}" longdesc="{description}" /></a>
</xsl:template>
<!-- End Logo template -->

<!-- Start RSS item template -->
<xsl:template match="//item">
    <h2><xsl:value-of select="title" /></h2>
    <p id="subjtime"><xsl:value-of select="dc:subject" /> / <xsl:value-of select="dc:pubDate" /></p>
    <p><xsl:value-of select="description" /></p>
    <p id="itemlink">[ <a href="{link}">more</a> ]</p>
</xsl:template>
<!-- End RSS item template -->
</xsl:stylesheet>

因此,<xsl:value-of select="//dc:date" />以及<xsl:value-of select="dc:subject" /><xsl:value-of select="dc:pubDate" />不会提取数据。

我对XML完全不熟悉,所以我显然遗漏了一些东西,我只是不知道是什么。我一直在寻找解决方案,但没有成功。

如果有人能指出我做错了什么,我们将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

您在XSLT中的dc命名空间声明的末尾缺少斜杠:

xmlns:dc="http://purl.org/dc/elements/1.1"

应该是:

xmlns:dc="http://purl.org/dc/elements/1.1/"

命名空间URI必须完全匹配。修复后,XSLT应成功检索您要访问的值。