Styling RSS feed using XSLT, issues with CDATA tags

隐身守侯 提交于 2019-12-25 08:57:44

问题


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>

You can see my attempt here

Here is my 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>

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?


回答1:


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

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

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

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>


来源:https://stackoverflow.com/questions/14912179/styling-rss-feed-using-xslt-issues-with-cdata-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!