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

3 string xslt

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

Mar*_*ell 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>
Run Code Online (Sandbox Code Playgroud)