0

我正在尝试从命名空间为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 完全陌生,所以我显然遗漏了一些东西,我只是不知道是什么。我四处寻找解决方案,但没有成功。

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

谢谢

4

1 回答 1

1

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

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

应该:

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

命名空间 URI 必须完全匹配。一旦解决了这个问题,XSLT 应该会成功检索您想要访问的值。

于 2013-04-23T19:46:23.740 回答