【问题标题】:I have an RSS feed that I'm transforming with XSLT in order to display on my home page as HTML. The XML for the feed looks like this:我有一个 RSS 提要,我正在使用 XSLT 进行转换,以便在我的主页上显示为 HTML。供稿的 XML 如下所示:
【发布时间】:2015-07-17 03:36:36
【问题描述】:

我有一个 RSS 提要,我正在使用 XSLT 对其进行转换,以便在我的主页上显示为 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>

使用相同的格式,我想获取&lt;htext&gt; 元素的内容并将其显示为与&lt;story&gt; 文本的其余部分内联的实际超链接。

<story>The Motion of Thought is Mr. Mutant's blog.  Check it out:<htext url="www.mutantcreations.com/Blog">Here</htext>.</story>

此外,我如何为每个 &lt;htext&gt; 中的每个 &lt;story&gt; 元素执行此操作?

【问题讨论】:

  • htext 不是超链接。您想要实际的超链接还是您发布的代码?

标签: html xml xslt rss


【解决方案1】:

不要使用&lt;xsl:value-of select="story" /&gt;,而是使用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>

【讨论】:

  • 像魅力一样工作。非常感谢,蒂姆 C。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 2015-12-01
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
相关资源
最近更新 更多