1

如何使用 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">…&lt;/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 专家的一些帮助将不胜感激..

4

1 回答 1

1

Here is a non-recursive, pure XSLT 1.0 solution:

<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>

When this transformation is applied on the following XML document:

<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>

the wanted, correct result is produced:

 story gist here..a couple of sentences. Many more sentences ...
even some more text with lots of meaning and sense
于 2013-02-17T01:20:48.143 回答