获取文本节点的最大起始子字符串,以使其中的最后一个单词不被截断,并且其长度不超过给定限制

时间:2013-02-17 00:00:15

标签: xslt xpath xslt-1.0

如何使用xsl仅选择n个注释元素。我可以选择n个字符,但是它会在一个单词的中间捕捉并使它看起来很难看。

我已经能够获得“评论”节点中单词总数的计数,但不知道如果总共有20个单词,那么如何只显示15个单词。

  <div class="NewsDescription">
  <xsl:value-of select="string-length(translate(normalize-space(@Comments),'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',''))+1" /> 
    <xsl:value-of select="substring(@Comments,0,120)"/>
    <xsl:if test="string-length(@Comments) &gt; 120">…</xsl:if>
    <a href="{$SafeLinkUrl}" class="ReadMore" style="font-size:11px ;font-family:Arial, Helvetica, sans-serif;color :#00aeef">Read More</a> 
   </div>

实际的xml是这样的。我试图在动态页面的右侧以“相关故事”的方式汇总故事片。

<news>
   <title>Story title</title>
    <comments> story gist here..a couple of sentences.</comments>
    <content> actualy story </content>

请帮忙。我通过Marc andersen找到了这些资源,但是xsl太复杂了,我无法过滤并利用自己。

http://sympmarc.com/2010/07/13/displaying-the-first-n-words-of-announcement-bodies-with-xsl-in-a-dvwp/

XSL专家的一些帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这是一个非递归的纯XSLT 1.0解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
 <xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
 <xsl:variable name="vDigits" select="'0123456789'"/>

 <xsl:variable name="vAlhanum" select="concat($vLower, $vUpper, $vDigits)"/>

 <xsl:template match="comments">
   <xsl:param name="pText" select="."/>
     <xsl:choose>
       <xsl:when test="not(string-length($pText) > 120)">
         <xsl:value-of select="$pText"/>
       </xsl:when>
         <xsl:otherwise>
           <xsl:variable name="vTruncated" select="substring($pText, 1, 121)"/>
             <xsl:variable name="vPunct" 
                           select="translate($vTruncated, $vAlhanum, '')"/>
           <xsl:for-each select=
           "(document('')//node() | document('')//@* | document('')//namespace::*)
                           [not(position() > 121)]">

             <xsl:variable name="vPos" select="122 - position()"/>
             <xsl:variable name="vRemaining" select="substring($vTruncated, $vPos+1)"/>

             <xsl:if test=
              "contains($vPunct, substring($vTruncated, $vPos, 1))
              and
               contains($vAlhanum, substring($vTruncated, $vPos -1, 1))
              and
                 string-length(translate($vRemaining, $vPunct, ''))
                         = string-length($vRemaining)
              ">
              <xsl:value-of select="substring($vTruncated, 1, $vPos -1)"/>
             </xsl:if>
           </xsl:for-each>
      </xsl:otherwise>
     </xsl:choose>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

对以下XML文档应用此转换时:

<news>
   <title>Story title</title>
    <comments> story gist here..a couple of sentences. Many more sentences ...
even some more text with lots of meaning and sense aaand a lot of humor. </comments>
    <content> actualy story </content>
</news>

产生了想要的正确结果

 story gist here..a couple of sentences. Many more sentences ...
even some more text with lots of meaning and sense