防止<cite>标记出现在RSS Feed </cite>中

时间:2013-12-05 15:56:23

标签: xslt coldfusion rss

我正在使用doctype:XHTML Mobile Profile 1.2,XML version =“1.0 and Content-Type”application / xhtml + xml“

是否可以禁用或阻止RSS Feed中出现<cite>标记,因为我一直在页面本身上出现此错误。

error on line 24 at column 70: expected '>'
Below is a rendering of the page up to the first error.

我正在使用来自其他网站的外部Feed,这不是我可以控制或编辑的。

我正在使用XSLT和ColdFusion文件来读取外部RSS文件,并在我的XSLT中以我想要的方式显示它,我已经到位disable-output-escaping="yes"以防止丢失代码显示在Feed中。当此标记不存在时,我的XSLT正常工作

我试图绕过它,但没有运气。实际上可以这样做吗?

CFM

<cfcontent type="application/xhtml+xml" reset="yes">
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">

<cfhttp method="Get" url="http://www.animenewsnetwork.com/news/rss.xml">
<cfset xmlInput = CFHTTP.FileContent>
<cfset MyXslFile = Expandpath("animenewsrss.xsl")>
<cffile action="READ" variable="xslInput" file="#MyXslFile#">
<cfset xmlOutput = XMLTransform(xmlInput, xslInput)>
<cfoutput>#xmloutput#</cfoutput>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" omit-xml-declaration="yes" />

<xsl:template match="rss/channel">
    <xsl:element name="html">
      <xsl:element name="head">
     <xsl:element name="title">Anime News</xsl:element>
      </xsl:element>
      <xsl:element name="body">
      <xsl:element name="div"><xsl:attribute name="id"><xsl:value-of select="'hstyle'"/></xsl:attribute>Media Events UK - Anime News</xsl:element>
      <xsl:element name="div"><xsl:attribute name="id"><xsl:value-of select="'nstyle'"/>
        </xsl:attribute><xsl:element name="a"><xsl:attribute name="href">index.cfm</xsl:attribute>Home</xsl:element> - 
        <xsl:element name="a"><xsl:attribute name="href">listings.cfm</xsl:attribute>Listings</xsl:element> - 
        <xsl:element name="a"><xsl:attribute name="href">venue.cfm</xsl:attribute>Venues</xsl:element>
        </xsl:element>
        <xsl:apply-templates select="item[position() &lt; 6]" />
      </xsl:element>
    </xsl:element>
</xsl:template>

<xsl:template match="item[position() &lt; 6]">
  <div class="rsstyle">
        <xsl:element name="a">
            <xsl:attribute name="href">
            <xsl:value-of select="link"/>
            </xsl:attribute>
            <xsl:value-of select="title" />
        </xsl:element>
        <xsl:element name="div">
        <xsl:value-of select="pubDate" />
        </xsl:element>
        <xsl:element name="div">
        <xsl:value-of select="concat(substring(description, 1, 50), '...')" disable-output-escaping="yes"/>
        </xsl:element>
</div>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:5)

使用此行

<xsl:value-of select="concat(substring(description, 1, 50), '...')" disable-output-escaping="yes"/>

在某些情况下,您剪切了<description>的内容,其中包含cite个元素。这导致结果HTML中的行如下所示:

<div><cite>Ni No Kuni</cite>, <cite>Tales of Xillia</ci...</div>

如您所见,cite元素不再正常关闭,因为您将description元素的内容剪切为50个字符。如果您对字符进行了计数,则会注意到description的内容在50处停止,然后插入“...”。

如果您描述了将子字符串应用于decription元素的意图,那么SO可以帮助您找到一个很好的替代方案。

我的猜测是,您需要考虑description不仅包含文字,还包含元素(如cite)的可能性。然后,仅在描述的文本内容上使用子字符串是有意义的,如下所示:

concat(substring(description/text(),1,50),'...')

然后继续捕捉description的子元素,例如在一个单独的模板中:

<xsl:template match="cite[parent::description]">
  <!--Deal with cite elements-->
</xsl:template>

编辑:我调整了您的样式表来处理cite元素作为description的子元素。有2个额外的模板可以处理文本节点和cite节点,这两个节点都是description的子节点。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" />

<xsl:template match="rss/channel">
  <!--I left this template unchanged!-->
</xsl:template>

<xsl:template match="item[position() &lt; 6]">
  <div class="rsstyle">
    <xsl:element name="a">
        <xsl:attribute name="href">
        <xsl:value-of select="link"/>
        </xsl:attribute>
        <xsl:value-of select="title" />
    </xsl:element>
    <xsl:element name="div">
    <xsl:value-of select="pubDate" />
    </xsl:element>
    <xsl:element name="div">
    <xsl:apply-templates select="description"/>
    </xsl:element>
  </div>
</xsl:template>

<xsl:template match="description">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()[parent::description]">
  <xsl:copy/>
</xsl:template>

<xsl:template match="cite[parent::description]">
  <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>
相关问题