函数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="'	 '" /> <!-- 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,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="' 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>
输出:
<test> <ltrim>test </ltrim> <rtrim> test</rtrim> <trim>test</trim> </test>