简体   繁体   English

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

[英]How to limit string word count in XSLT 1.0?

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

How about something like: 怎么样的:

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

This is an XSLT 1.0 solution : 这是一个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>

when this transformation is applied on the following XML document: 当此转换应用于以下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>

the wanted result is returned : 返回想要的结果

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

Do note : 请注意

  1. The str-split-to-words template from FXSL is used for tokenization. str-split-to-words 从模板 FXSL 用于标记化。

  2. This template accepts a parameter pDelimiters which is a string consisting of all characters that should be treated as delimiters. 此模板接受参数pDelimiters ,该参数是由应视为分隔符的所有字符组成的字符串。 Thus, in contrast with other solutions, it is possible to specify every delimiter (and not just a "space") -- in this case 8 of them. 因此, 与其他解决方案相比,可以指定每个定界符 (而不仅仅是“空格”) - 在这种情况下为8个定界符

  3. The named template strTakeWords calls itself recursively to accumulate the text before and including every word from the wordlist produced by the tokenization, until the specified number of words has been processed. 命名模板strTakeWords以递归方式调用自身,以便在标记化生成的单词列表中包含每个单词之前累积文本 ,直到处理完指定的单词数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM