我使用rss feed获取新闻项,然后使用XSLT将它们显示给用户。但是RSS源在描述节点中有<br>
个标签。我想删除它们,以便文本正确包装。
XML:
<item>
<title>This is sample text</title>
<link>http://service.meltwaternews.com/mnews/redirect.html?docId=3724026852&userId=327550&cId=88306&agentId=5799665&type=1&s=83181&
url=http%3A%2F%2Fwww.law360.com%2Farticles%2F596778</link>
<description>M&A MVP: <b>Wachtell</b>'s Andy Brownstein -- Thanks to his work over the past<br> year
guiding numerous multibillion-dollar deals that spanned a number<br>...<br></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&
userId=327550&cId=88306&agentId=5799665&type=1&s=54&
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
& Co. and its legal advisor<br> is <b>Wachtell</b>, <b>Lipton</b>, Rosen & Katz. About BGC Partners, Inc. BGC Partners<br>...<br></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) > number($ITEMSPERPAGE)">
<xsl:text disable-output-escaping="yes"><SCRIPT>
function getNewPagingUrl(parameter, pageNum) { var parms = document.URL.split(/[&?]/); if (parms.length < 2) { return document.URL + "?" + parameter + "=" + pageNum; } else { var finalParms = new Array();var fnd = false; for (var i=0; i<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<finalParms.length;i++) { if (i==0) finalUrl += finalParms[i]; else if (i==1) finalUrl += "?" + finalParms[i]; else finalUrl += "&" + finalParms[i]; } return finalUrl; } }
</SCRIPT>
</xsl:text>
<xsl:text disable-output-escaping="yes"><TABLE width="100%" ><TR><TD align="right"></xsl:text>
<xsl:variable name="ttlPages">
<xsl:choose>
<xsl:when test="count(channel/item) > $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"></TABLE></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()<=$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">&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>
有人可以帮我解决这个问题。如果您需要更多信息,请与我们联系。
感谢。
答案 0 :(得分:0)
请详细说明。
你打电话给什么&#34; <br> tags
&#34;实际上是无意义的<br>
字符串(就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>