我有一个RSS源,我正在使用XSLT进行转换,以便在我的主页上显示为HTML。 Feed的XML如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="News.css" ?>
<rss version="2.0">
<channel>
<title>Mutant Creations - News</title>
<link>http://www.mutantcreations.com</link>
<description>Don't worry, I'll distract the nerds. Just get back to the ship!</description>
<item>
<title>Blog Pie</title>
<pubDate>Thursday, 26 Mar 2015 21:43:00 PT</pubDate>
<link>http://www.mutantcreations.com</link>
<description>Blog is up.</description>
<story>The Motion of Thought is Mr. Mutant's blog. Check it out:<htext url="www.mutantcreations.com/Blog">Here</htext>.</story>
</item>
</channel>
</rss>
我的XSL看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>News!</h2>
<xsl:for-each select="rss/channel/item">
<div style="color: #100000; padding:4px;">
<span style="font-weight:bold; text-decoration: underline;">
<xsl:value-of select="title"/>
</span>
</div>
<div style="color: #200000; padding:2px; font-size:11px;">
<span>
<xsl:value-of select="pubDate"/>
</span>
</div>
<div style="margin: 1em 0 1em 2em; margin-right:25px;font-size:12pt;">
<span>
<xsl:value-of select="story"/>
</span>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
使用相同的格式,我想获取<htext>
元素的内容,并将其显示为与<story>
文本的其余部分内联的实际超链接。
<story>The Motion of Thought is Mr. Mutant's blog. Check it out:<htext url="www.mutantcreations.com/Blog">Here</htext>.</story>
此外,我如何为每个<htext>
中的每个<story>
元素执行此操作?
答案 0 :(得分:0)
而不是<xsl:value-of select="story" />
使用xsl:apply-templates
<xsl:apply-templates select="story"/>
XSLT的内置模板可用于匹配story
(因此story
元素不会出现在输出本身中)所以您只需要一个匹配的模板htext
改变它。
<xsl:template match="htext">
<a href="{@url}">
<xsl:apply-templates />
</a>
</xsl:template>
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>News!</h2>
<xsl:for-each select="rss/channel/item">
<div style="color: #100000; padding:4px;">
<span style="font-weight:bold; text-decoration: underline;">
<xsl:value-of select="title"/>
</span>
</div>
<div style="color: #200000; padding:2px; font-size:11px;">
<span>
<xsl:value-of select="pubDate"/>
</span>
</div>
<div style="margin: 1em 0 1em 2em; margin-right:25px;font-size:12pt;">
<span>
<xsl:apply-templates select="story"/>
</span>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="htext">
<a href="{@url}">
<xsl:apply-templates />
</a>
</xsl:template>
</xsl:stylesheet>
应用于输入样本时,输出以下内容
<html>
<body>
<h2>News!</h2>
<div style="color: #100000; padding:4px;"><span style="font-weight:bold; text-decoration: underline;">Blog Pie</span></div>
<div style="color: #200000; padding:2px; font-size:11px;"><span>Thursday, 26 Mar 2015 21:43:00 PT</span></div>
<div style="margin: 1em 0 1em 2em; margin-right:25px;font-size:12pt;"><span>The Motion of Thought is Mr. Mutant's blog. Check it out:<a href="www.mutantcreations.com/Blog">Here</a>.</span></div>
</body>
</html>