我正在尝试学习如何制作 RSS 提要,并且正在尝试制作 xslt 和 css 部分。我需要 rss 提要来显示每个元素的标题、描述、长度和 pubDate,我正在尝试为此使用 CSS。
这是我目前所拥有的(抱歉,不是很好):
CSS 文件(rotten.css):
@charset "UTF-8";
/* CSS Document */
h2 {
color:#03C;
}
.intro {
font-style:italic;
color:#666
}
xslt 文件 (rotten.xslt):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0
Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1- transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title><xsl:value-of select="/rss/channel/title" /></title>
<!-- create a link to the CSS stylesheet -->
<link type="text/css" rel="stylesheet" href="rotten.css" />
</head>
<body>
<!--print out the title and description of the feed. -->
<h2><xsl:value-of select="/rss/channel/title" /></h2>
<p class="intro"><xsl:value-of select="/rss/channel/description" /></p>
<hr />
<!-- print out the news stories -->
<xsl:apply-templates select="/rss/channel/item" />
</body>
</html>
</xsl:template>
<xsl:template match="/rss/channel/item">
<!-- put HTML and XSLT for displaying the stories here -->
</xsl:template>
</xsl:stylesheet>
我需要添加什么才能使它们协同工作?或者...我是否完全搞砸了并且无法恢复?
我知道 RSS 提要的基本结构看起来像这样(减去实际数据):
<rss>
<channel>
<title>Name of feed</title>
<link>URL of rss feed xml file</link>
<description>Description of feed</description>
<item>
<title>Item1 title</title>
<link>Item1 URL</link>
<description>Item1description</description>
</item>
</channel>
</rss>
我认为应该放在 xslt 文件中,但我不确定放在哪里...
最佳答案
在您的 HTML 输出上获得数据之前,先忘掉 CSS - 您可以稍后再回来使用它。
显示每个元素的代码应该放在<xsl:template match="/rss/channel/item">
中。您在 XSLT 底部创建的模板。例如(假设您的 XML 片段是正确的),您可以通过像这样修改模板来为每个提要获得一个非常基本的、以连字符分隔的输出:
<xsl:template match="/rss/channel/item">
<p><!-- wrap the output in a p so it appears on one line per item -->
<xsl:apply-templates select="title"/> -
<xsl:apply-templates select="link"/> -
<xsl:apply-templates select="description"/>
</p>
</xsl:template>
此模板将针对 rss/ channel 中的每个元素自动调用,因此您应该获得列表中的所有元素。然后,您可以使用您喜欢的任何 HTML 或 CSS 来装饰它,并且可能进一步研究 XSLT 以找到使用模板来获得您想要的内容的更好方法。
但希望这能让您入门。
关于css - 首先尝试使用 XML 和 CSS 的 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8425512/