3

私はこれを変数の下に持っています

  <xsl:variable name="testvar">
        d 
        e 
        d
    </xsl:variable>

そして私はこの機能を持っています:

    <xsl:choose>
        <xsl:when test="not($str-input)">
            <func:result select="false()"/>
        </xsl:when>
        <xsl:otherwise>
            <func:result select="translate($str-input,$new-line,'_')"/>
        </xsl:otherwise>
    </xsl:choose>
</func:function>

関数をテストしたところ、結果は次のようになりました: _ d _ e _ d_ そして、結果を

d_e_d

4

2 に答える 2

4

XSLT 1.0の場合:

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

  <xsl:variable name="new-line" select="'&#10;'" />

  <xsl:variable name="str-input">
        d 
        e 
        d
  </xsl:variable>

  <!-- your <xsl:choose>, slightly modified -->    
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="not($str-input)">
        <xsl:value-of select="false()"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="temp">
          <xsl:call-template name="normalize-newline">
            <xsl:with-param name="str" select="$str-input" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="translate($temp, $new-line, '_')" />
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

  <!-- a template that trims leading and trailing newlines -->    
  <xsl:template name="normalize-newline">
    <xsl:param name="str" select="''" />

    <xsl:variable name="temp" select="concat($str, $new-line)" />
    <xsl:variable name="head" select="substring-before($temp, $new-line)" />
    <xsl:variable name="tail" select="substring-after($temp, $new-line)" />
    <xsl:variable name="hasHead" select="translate(normalize-space($head), ' ', '') != ''" />
    <xsl:variable name="hasTail" select="translate(normalize-space($tail), ' ', '') != ''" />

    <xsl:if test="$hasHead">
      <xsl:value-of select="$head" />
      <xsl:if test="$hasTail">
        <xsl:value-of select="$new-line" />
      </xsl:if>
    </xsl:if>
    <xsl:if test="$hasTail">
      <xsl:call-template name="normalize-newline">
        <xsl:with-param name="str" select="$tail" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

戻り値:

"        d _        e _        d"

スペースは変数値の一部です。を使用して削除することもできますが、実際には何であるかnormalize-space()わからないため、変更しないでおきます。"d""e"

于 2009-09-29T14:34:49.770 に答える
0

変数を次のように変更できますか:

<xsl:variable name="testvar">
        d 
        e 
        d</xsl:variable>

?

于 2009-09-29T14:08:57.687 に答える