使用 XSLT 设置 RSS 提要样式,CDATA 标签问题 [英] Styling RSS feed using XSLT, issues with CDATA tags

查看:52
本文介绍了使用 XSLT 设置 RSS 提要样式,CDATA 标签问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 RSS 提要设置样式,但在以下部分存在问题:

I am styling an RSS feed but having an issue with the following part:

<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br /> <b>Current Conditions:</b><br /> Fair, 73 F<BR /> <BR /><b>Forecast:</b><BR /> Sat - Clear. High: 78 Low: 62<br /> Sun - Mostly Sunny. High: 80 Low: 66<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]>
</description>

您可以在此处

这是我的 XSLT:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
  <head>
    <title></title>
  </head>
  <body>
    <table cellpadding="2" cellspacing="0" border="0" width="75%">
        <xsl:for-each select="rss/channel/item">

              <tr style="color:#0080FF;">
                <td style="text-align:left;font-weight:bold;">
                  <xsl:value-of select ="title"></xsl:value-of>
                </td>
                </tr>


                <tr style="color:#0080FF;">
                <td style="text-align:left;font-weight:bold;">
                  <xsl:value-of select ="location"></xsl:value-of>
                  <xsl:value-of select="pubDate"/>
                </td>
              </tr>


              <tr>
                <td colspan="2" style="text-align:left;padding-top:10px;">
                  <xsl:value-of select="description"/>
                </td>
              </tr>

          <tr>
            <td colspan="2" style="height:20px;">
              <hr></hr>
            </td>
          </tr>
        </xsl:for-each>
    </table>
  </body>
</html>
 </xsl:template>
</xsl:stylesheet>

我试图从描述标签中获取信息,这样我就可以像标题和出版日期一样设置样式.这里是我正在尝试设置样式的完整 XML RSS 提要.谁能帮我弄清楚为什么 CDATA 标签会搞砸?

I am trying to get the information from the description tag so I can style it like I did the title and publication date. Here is the full XML RSS feed which I am trying to style. Can anyone help me figure out why the CDATA tags are messing things up?

推荐答案

尽量不要使用 .当您依赖 时,代码会变得更加清晰.

Try not to use <xsl:for-each>. Code becomes more clearly arranged when you rely on <xsl:template> and <xsl:apply-templates>.

还尝试使用 CSS 类并从输出 HTML 中删除内联样式.

Also try to use CSS classes and remove the inline style from the output HTML.

如果您输出经典的 HTML(不是 XHTML),那么使用 <xsl:output> 告诉 XSLT 处理器并输出一个文档类型.

If you output classic HTML (not XHTML) then tell the XSLT processor so by using <xsl:output> and also output a doctype.

您的输出问题将通过使用 disable-output-escaping="yes" 来解决.请注意,并非每个 XSL 处理器都支持该属性.根据 XSLT 规范,禁用输出转义的能力是可选的.

Your output problem will be solved by using disable-output-escaping="yes". Note that not every XSL processor supports that attribute. The ability to disable output escaping is optional as per the XSLT spec.

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="html" indent="yes" 
     doctype-system='http://www.w3.org/TR/html4/strict.dtd'
     doctype-public='-//W3C//DTD HTML 4.01//EN' 
  />

  <xsl:template match="/rss">
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <xsl:apply-templates select="channel" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="channel">
    <table cellpadding="2" cellspacing="0" border="0" width="75%">
      <xsl:apply-templates select="item" />
    </table>
  </xsl:template>

  <xsl:template match="item">
    <!-- ... -->
    <tr>
      <td colspan="2" style="text-align:left;padding-top:10px;">
        <xsl:value-of select="description" disable-output-escaping="yes" />
      </td>
    </tr>
    <!-- ... -->
  </xsl:template>
</xsl:stylesheet>

这篇关于使用 XSLT 设置 RSS 提要样式,CDATA 标签问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆