如何在不用单个空格替换重复空格的情况下修剪XSLT中的空间?

Mat*_*der 15 xml xslt xpath

该函数normalize-space用单个空格替换空格序列,并修剪提供的字符串.如何在不更换空格的情况下修剪字符串?有专有的解决方案orcl:left-trim,但我正在寻找一个非专有的解决方案.

例:

<xsl:value-of select="trim(/Car/Description)"/>
Run Code Online (Sandbox Code Playgroud)

应该转

<car>
  <description>  To get more information look at:     www.example.com </description>
</car>
Run Code Online (Sandbox Code Playgroud)

"To get more information look at:     www.example.com"
Run Code Online (Sandbox Code Playgroud)

Jör*_*ann 16

仅使用xslt 1.0模板的解决方案:

<xsl:variable name="whitespace" select="'&#09;&#10;&#13; '" />

<!-- Strips trailing whitespace characters from 'string' -->
<xsl:template name="string-rtrim">
    <xsl:param name="string" />
    <xsl:param name="trim" select="$whitespace" />

    <xsl:variable name="length" select="string-length($string)" />

    <xsl:if test="$length &gt; 0">
        <xsl:choose>
            <xsl:when test="contains($trim, substring($string, $length, 1))">
                <xsl:call-template name="string-rtrim">
                    <xsl:with-param name="string" select="substring($string, 1, $length - 1)" />
                    <xsl:with-param name="trim"   select="$trim" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
</xsl:template>

<!-- Strips leading whitespace characters from 'string' -->
<xsl:template name="string-ltrim">
    <xsl:param name="string" />
    <xsl:param name="trim" select="$whitespace" />

    <xsl:if test="string-length($string) &gt; 0">
        <xsl:choose>
            <xsl:when test="contains($trim, substring($string, 1, 1))">
                <xsl:call-template name="string-ltrim">
                    <xsl:with-param name="string" select="substring($string, 2)" />
                    <xsl:with-param name="trim"   select="$trim" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$string" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:if>
</xsl:template>

<!-- Strips leading and trailing whitespace characters from 'string' -->
<xsl:template name="string-trim">
    <xsl:param name="string" />
    <xsl:param name="trim" select="$whitespace" />
    <xsl:call-template name="string-rtrim">
        <xsl:with-param name="string">
            <xsl:call-template name="string-ltrim">
                <xsl:with-param name="string" select="$string" />
                <xsl:with-param name="trim"   select="$trim" />
            </xsl:call-template>
        </xsl:with-param>
        <xsl:with-param name="trim"   select="$trim" />
    </xsl:call-template>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

测试代码:

<ltrim>
    <xsl:call-template name="string-ltrim">
        <xsl:with-param name="string" select="'   &#10;   test  '" />
    </xsl:call-template>
</ltrim>
<rtrim>
    <xsl:call-template name="string-rtrim">
        <xsl:with-param name="string" select="'    &#10;    test  &#10;  '" />
    </xsl:call-template>
</rtrim>
<trim>
    <xsl:call-template name="string-trim">
        <xsl:with-param name="string" select="'    &#10;    test  &#10;  '" />
    </xsl:call-template>
</trim>
Run Code Online (Sandbox Code Playgroud)

输出:

<test>
    <ltrim>test  </ltrim>
    <rtrim>   
    test</rtrim>
    <trim>test</trim>
</test>
Run Code Online (Sandbox Code Playgroud)

  • @MathiasBader:`xsl:strip-space`在任何转换之前对源文档进行操作,并删除仅包含空格的文本节点.它可能无法解决您的问题.最简单的解决方案可能是创建一个单独的转换,仅修剪所选元素的空白,并将其结果用作第二个转换的输入.这可以在单个样式表中使用特定处理器的扩展函数完成,或使用外部工具/ api调用转换. (2认同)

小智 5

Normalize-space(actualSting) - 这样就可以了。

  • 这也会删除中间重复的空格。我一直在寻找一种不这样做的解决方案。 (3认同)