我有一个RSS提要(下面的链接),我想限制标题节点中返回的文本的长度
RSS提要:https : //news.google.co.uk/news?pz=1&cf=all&ned=uk&hl= zh-CN &q=uk&output= rss
所以我为限制添加了一个参数并将其设置为20
然后,我添加了一些测试语句来检查标题节点的长度是否大于/小于20。
我的问题是小于测试总是输出-即使标题大于20!
<xsl:stylesheet version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:param name="allowable-length" select="20"/>
<ul>
<xsl:for-each select="rss/channel/item[position() <= 6]">
<xsl:sort select="pubDate" order="ascending"/>
<xsl:if test="string-length(rss/channel/item/title) <= $allowable-length">
<li class="news">
<a href="{link}" title="{title}">
<xsl:value-of select="title"/>
</a>
</li>
</xsl:if>
<xsl:if test="string-length(rss/channel/item/title) > $allowable-length">
<li class="news">
<a href="{link}" title="{title}">
<xsl:value-of select="substring(title, 1, 20)"/>
... </a>
</li>
</xsl:if>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
无效的主要原因是里面的XPathfor-each
是相对的item
,但是你的路径开始于rss
。
您也可以通过消除重复来大大清理XSLT。试试看。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="allowable-length" select="20"/>
<xsl:template match="/">
<ul>
<xsl:apply-templates select="rss/channel/item[position() <= 6]">
<xsl:sort select="pubDate" order="ascending"/>
</xsl:apply-templates>
</ul>
</xsl:template>
<xsl:template match="item">
<li class="news">
<a href="{link}" title="{title}">
<xsl:value-of select="substring(title, 1, $allowable-length)"/>
<xsl:if test="string-length(title) > $allowable-length">
<xsl:text>...</xsl:text>
</xsl:if>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句