<br> 从 RSS 源中移除代码



我正在使用rss提要来获取新闻项,然后使用XSLT将它们显示给用户。但是 rss 提要在描述节点中具有<br>标记。我想删除它们,以便文本正确换行。

来自 rss feed的 XML:

<item>
         <title>This is sample text</title>
         <link>http://service.meltwaternews.com/mnews/redirect.html?docId=3724026852&amp;userId=327550&amp;cId=88306&amp;agentId=5799665&amp;type=1&amp;s=83181&amp;
url=http%3A%2F%2Fwww.law360.com%2Farticles%2F596778</link>
         <description>M&amp;A MVP: &lt;b&gt;Wachtell&lt;/b&gt;'s Andy Brownstein -- Thanks to his work over the past&lt;br&gt; year 
guiding numerous multibillion-dollar deals that spanned a number&lt;br&gt;...&lt;br&gt;</description>
         <pubDate>Thu, 20 Nov 2014 18:20:42 GMT</pubDate>
         <source url="http://www.law360.com/">Law360</source>
      </item>
      <item>
         <title>BGC Partners Extends Tender Offer To Acquire GFI Group</title>
         <link>http://service.meltwaternews.com/mnews/redirect.html?docId=3723113040&amp;
userId=327550&amp;cId=88306&amp;agentId=5799665&amp;type=1&amp;s=54&amp;
url=http%3A%2F%2Fwww.prnewswire.com%2Fnews-releases%2Fbgc-partners-extends-tender-offer-to-acquire-gfi-group-283330231.html</link>
         <description>...for the tender offer is Cantor Fitzgerald 
&amp; Co. and its legal advisor&lt;br&gt; is &lt;b&gt;Wachtell&lt;/b&gt;, &lt;b&gt;Lipton&lt;/b&gt;, Rosen &amp; Katz. About BGC Partners, Inc. BGC Partners&lt;br&gt;...&lt;br&gt;</description>
         <pubDate>Thu, 20 Nov 2014 13:56:48 GMT</pubDate>
         <source url="http://www.prnewswire.com/">PR Newswire</source>
      </item>

XSLT:

    <?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:msdndomain="http://msdn.microsoft.com/aboutmsdn/rss/domains"
    version="1.0" >

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match='node()|@*'>
     <xsl:copy>
       <xsl:apply-templates select='node()|@*'/>
     </xsl:copy>
 </xsl:template>
 <xsl:template match='br[preceding-sibling::node()[1]
    [not(self::text() and normalize-space(.) = "")][self::br]]'/>


<xsl:param name="maxitems">30</xsl:param>
<xsl:param name="showtitle">False</xsl:param>
<xsl:param name="totalitems"><xsl:value-of select="count(/rss/channel/item)"/></xsl:param>
        <xsl:param name = "Postback.CNUSERPAGE" >0</xsl:param>
    <xsl:param name = "ITEMSPERPAGE" >5</xsl:param>


    <xsl:template match='/rss'>
        <xsl:if test="count(channel/item) &gt; number($ITEMSPERPAGE)">
            <xsl:text disable-output-escaping="yes">&lt;SCRIPT&gt;
            function getNewPagingUrl(parameter, pageNum) {  var parms = document.URL.split(/[&amp;?]/);  if (parms.length &lt; 2) {     return document.URL + "?" + parameter + "=" + pageNum; } else {     var finalParms = new Array();var fnd = false;   for (var i=0; i&lt;parms.length;i++)    {       if (parms[i].search(parameter+"=") != -1)       {           fnd=true;finalParms[i] = parameter + "=" + pageNum;         }       else        {           finalParms[i] = parms[i];       }   }   if(!fnd){finalParms[parms.length]=parameter + "=" + pageNum;}
    var finalUrl = "";  for (var i=0; i&lt;finalParms.length;i++)   {       if (i==0)           finalUrl += finalParms[i];      else if (i==1)          finalUrl += "?" + finalParms[i];        else            finalUrl += "&amp;" + finalParms[i];    }   return finalUrl; } }
            &lt;/SCRIPT&gt;
        </xsl:text>
            <xsl:text disable-output-escaping="yes">&lt;TABLE width="100%" &gt;&lt;TR&gt;&lt;TD align="right"&gt;</xsl:text>
<xsl:variable name="ttlPages">
<xsl:choose>
        <xsl:when test="count(channel/item) &gt; $maxitems">
            <xsl:value-of select="$maxitems"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="count(channel/item)"/>    
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
            <xsl:call-template name="paging">
                <xsl:with-param name="repeat" select="floor($ttlPages div number($ITEMSPERPAGE))"/>
                <xsl:with-param name="totalPages" select="floor($ttlPages div number($ITEMSPERPAGE))"/>
            </xsl:call-template>
            <xsl:text disable-output-escaping="yes">&lt;/TABLE&gt;</xsl:text>
        </xsl:if>
        <div id='block' style="padding:5px;">
            <xsl:if test="$showtitle='True'">
                <div style="font-size:100%;">
                    <xsl:value-of select='channel/title'/>
                </div>
                <hr style="height:1px;color:black;" />
            </xsl:if>
            <xsl:apply-templates select='channel/item[position()&lt;=$maxitems]'/>
            </div>
    </xsl:template>
    <xsl:template match='item'>
        <xsl:if test="floor((position()-1) div number($ITEMSPERPAGE)) = number($Postback.CNUSERPAGE)" >
            <div style="font-weight:bold;font-size:8pt;">
            <a style="font-size: 8pt" href='{link} ' target="_blank">
                <xsl:value-of select='title'/>
            </a>
        </div>
        <xsl:value-of select='description' disable-output-escaping='yes'  />
        <i><xsl:value-of select='dc:creator' />   <xsl:value-of select='pubDate' /></i>
        <br/>
        <br/>
        </xsl:if>
    </xsl:template>
    <xsl:template name="paging">
        <xsl:param name="totalPages">0</xsl:param>
        <xsl:param name="repeat">0</xsl:param>
        <xsl:if test="number($repeat) >= 1">
            <xsl:if test="($totalPages - $repeat) = number($Postback.CNUSERPAGE)">
                <xsl:value-of select="($totalPages - $repeat)+1"/>
            </xsl:if>
            <xsl:if test="($totalPages - $repeat) != number($Postback.CNUSERPAGE)">
                <a>
                    <xsl:attribute name="href">
                        <xsl:text disable-output-escaping="yes">javascript:</xsl:text>
                        <postback>
                            <column name="CNUSERPAGE">
                                <xsl:value-of select="($totalPages - $repeat)"/>
                            </column>
                        </postback>
                    </xsl:attribute>
                    <xsl:value-of select="($totalPages - $repeat) + 1"/>
                </a>
            </xsl:if>
            <xsl:if test="number($repeat) > 1">
                <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
            </xsl:if>
            <xsl:call-template name="paging">
                <xsl:with-param name="repeat" select="$repeat - 1"/>
                <xsl:with-param name="totalPages" select="$totalPages"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

有人可以帮我解决这个问题吗?如果您需要更多信息,请告诉我。

谢谢。

你能详细说明一下吗?

你所说的"<br> tags"实际上是无意义的&lt;br&gt;字符串(在XSLT方面毫无意义)。从说明中删除它们的唯一方法是使用字符串操作。如果使用的是 XSLT 1.0,则调用递归命名模板以删除字符串的所有匹配项 - 例如:

<xsl:template name="remove">
    <xsl:param name="text"/>
    <xsl:param name="searchString"/>
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:value-of select="substring-before($text,$searchString)"/>
            <xsl:call-template name="remove">
                <xsl:with-param name="text" select="substring-after($text,$searchString)"/>
                <xsl:with-param name="searchString" select="$searchString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

相关内容

  • 没有找到相关文章

最新更新