如何限制XSLT 1.0中的字符串字数?

时间:2009-04-29 13:31:40

标签: xslt string

如何在XSLT 1.0中限制字符串的字数?

2 个答案:

答案 0 :(得分:6)

如下:

  <xsl:template match="data"> <!-- your data element or whatever -->
    <xsl:call-template name="firstWords">
      <xsl:with-param name="value" select="."/>
      <xsl:with-param name="count" select="4"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="firstWords">
    <xsl:param name="value"/>
    <xsl:param name="count"/>

    <xsl:if test="number($count) >= 1">
      <xsl:value-of select="concat(substring-before($value,' '),' ')"/>
    </xsl:if>
    <xsl:if test="number($count) > 1">
      <xsl:variable name="remaining" select="substring-after($value,' ')"/>
      <xsl:if test="string-length($remaining) > 0">
        <xsl:call-template name="firstWords">
          <xsl:with-param name="value" select="$remaining"/>
          <xsl:with-param name="count" select="number($count)-1"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>
  </xsl:template>

答案 1 :(得分:4)

这是一个XSLT 1.0解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
>

   <xsl:import href="strSplit-to-Words.xsl"/>

   <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
      <xsl:variable name="vwordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="/"/>
          <xsl:with-param name="pDelimiters" 
                          select="', &#9;&#10;&#13;()-'"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:call-template name="strTakeWords">
        <xsl:with-param name="pN" select="10"/>
        <xsl:with-param name="pText" select="/*"/>
        <xsl:with-param name="pWords"
             select="ext:node-set($vwordNodes)/*"/>
      </xsl:call-template>
    </xsl:template>

    <xsl:template match="word" priority="10">
      <xsl:value-of select="concat(position(), ' ', ., '&#10;')"/>
    </xsl:template>

    <xsl:template name="strTakeWords">
      <xsl:param name="pN" select="10"/>
      <xsl:param name="pText"/>
      <xsl:param name="pWords"/>
      <xsl:param name="pResult"/>

      <xsl:choose>
          <xsl:when test="not($pN > 0)">
            <xsl:value-of select="$pResult"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="vWord" select="$pWords[1]"/>
            <xsl:variable name="vprecDelims" select=
               "substring-before($pText,$pWords[1])"/>

            <xsl:variable name="vnewText" select=
                "concat($vprecDelims, $vWord)"/>

              <xsl:call-template name="strTakeWords">
                <xsl:with-param name="pN" select="$pN -1"/>
                <xsl:with-param name="pText" select=
                      "substring-after($pText, $vnewText)"/>
                <xsl:with-param name="pWords" select=
                     "$pWords[position() > 1]"/>
                <xsl:with-param name="pResult" select=
                 "concat($pResult, $vnewText)"/>
              </xsl:call-template>
          </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

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

<t>
(CNN) -- Behind closed doors in recent days,
senior White House aides have been saying that
measuring President Obama's first 100 days
is the journalistic equivalent of a Hallmark holiday.
</t>

返回想要的结果

<强> (CNN) -- Behind closed doors in recent days, senior White House

请注意

  1. 来自 str-split-to-words FXSL 模板用于标记化。

    < / LI>
  2. 此模板接受参数pDelimiters,该参数是一个字符串,由应被视为分隔符的所有字符组成。因此,与其他解决方案相比,可以指定每个分隔符(而不仅仅是“空格”) - 在这种情况下为8个。

  3. 命名模板strTakeWords以递归方式调用自身,以便在标记化生成的单词列表之前和之前累积文本,直到处理完指定的单词数量为止