xml - 如何在不将重复空格替换为单个空格的情况下修剪 XSLT 中的空格?

标签 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"

最佳答案

仅使用 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>

关于xml - 如何在不将重复空格替换为单个空格的情况下修剪 XSLT 中的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974247/

相关文章:

python - 如何在 Python 中解析损坏的 XML?

xml - xml cdata返回XML解析失败: syntax error illegal byte sequence in encoding with xslt

xslt - 有没有办法在 xsl :if test? 中使用模式

Java XPath 获取具有特定标签匹配特定文本的所有节点

r - 如何仅从父HTML节点(子节点除外)提取文本?

javascript - 用 PHPWord setValue 函数替换 html 实体

xml - 如何通过 namespace 定位 XML 架构 (XSD)?

java - JAXB:编码的 XML 中缺少具体类型信息 (xsi:type)

xml - windows下XSLT样式表相对路径问题

XPath 条件返回 "wrong"节点