该函数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="'	 '" />
<!-- 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 > 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) > 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="' test '" />
</xsl:call-template>
</ltrim>
<rtrim>
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string" select="' test '" />
</xsl:call-template>
</rtrim>
<trim>
<xsl:call-template name="string-trim">
<xsl:with-param name="string" select="' test '" />
</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)