我有一个 RSS 提要,我正在使用 XSLT 对其进行转换,以便在我的主页上显示为 HTML。源的 XML 如下所示:



我有一个我使用XSLT转换的RSS feed,以便在我的主页上显示为HTML。提要的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>

此外,我该如何为每个 <story>中的每个 <htext>元素做到这一点?

而不是使用<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>

最新更新