Mar*_*nen 14
normalize-space(Datas/Data[@key='Name']/string)可能就足够了,它将修剪空白区域以及开始和结束.然而它也会将任何空白区域折叠到一个空间,我不知道你是否想要这样.
最简单的方法是使用FXSLtrim的模板功能。
这种转变:
<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="/">
'<xsl:call-template name="trim">
<xsl:with-param name="pStr" select="string(/*)"/>
</xsl:call-template>'
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于此 XML 文档时:
<someText>
This is some text
</someText>
Run Code Online (Sandbox Code Playgroud)
产生想要的正确结果:
'This is some text'
Run Code Online (Sandbox Code Playgroud)
trim工作原理:
消除字符串中所有起始空白字符很容易,但困难的是消除所有结束空白字符。
FXSL 的trim函数/模板通过使用 FXSL 的另一个模板/函数来反转字符串来实现此目的。
所以,处理过程是这样的:
消除前导空白。
反转结果。
消除前导空格。
最后逆转。
FXSL 2.0(针对 XSLT 2.0)的完整代码可以trim() 在此处查看。trim它与FXSL 1.0(针对 XSLT 1.0)模板的代码几乎相同。