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

时间:2012-12-20 14:24:58

标签: xml xslt xpath

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

示例:

<xsl:value-of select="trim(/Car/Description)"/>

应该转

<car>
  <description>  To get more information look at:     www.example.com </description>
</car>

"To get more information look at:     www.example.com"

5 个答案:

答案 0 :(得分: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>

测试代码:

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

输出:

<test>
    <ltrim>test  </ltrim>
    <rtrim>   
    test</rtrim>
    <trim>test</trim>
</test>

答案 1 :(得分:3)

XSLT1的一个非常简短的解决方案:

<xsl:template name="trim">
            <xsl:param name="str"/>

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

答案 2 :(得分:2)

使用FXSL(XSLT函数式编程的开源库,完全用XSLT编写),只需编写

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

  <xsl:output method="text"/>
  <xsl:template match="/*/description">
    '<xsl:call-template name="trim">
        <xsl:with-param name="pStr" select="."/>
    </xsl:call-template>'
  </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<car>
  <description>  To get more information look at:     www.example.com </description>
</car>

产生了想要的正确结果:

'To get more information look at:     www.example.com'

trim模板如何运作

它修剪左前方空格,然后反转生成的字符串并修剪其前导空格,然后最终反转生成的字符串。


<强> II。 XPath 2.0解决方案:

使用

replace(replace(/*/description, '^\s*(.+?)\s*$', '$1'), '^ .*$', '')

以下是基于XSLT - 2.0的验证:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     "<xsl:sequence 
      select="replace(replace(/*/description, '^\s*(.+?)\s*$', '$1'), '^ .*$', '')"/>"
 </xsl:template>
</xsl:stylesheet>

当在提供的XML文档(上面)上应用此转换时,将评估XPath表达式并将此评估的结果复制到输出:

 "To get more information look at:     www.example.com"

答案 3 :(得分:2)

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

答案 4 :(得分:-2)

如果您在中间没有任何空格,您只需使用:

   
translate(/Car/Description,' ','')