从rss Feed中删除了标签

时间:2014-12-01 18:44:25

标签: xml xslt xslt-1.0

我使用rss feed获取新闻项,然后使用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>

有人可以帮我解决这个问题。如果您需要更多信息,请与我们联系。

感谢。

1 个答案:

答案 0 :(得分:0)

  

请详细说明。

你打电话给什么&#34; <br> tags&#34;实际上是无意义的&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>