Jak*_*kob 5 xslt xpath xslt-1.0 removing-whitespace
该函数normalize-space删除前导和尾随空格,并用单个空格替换空白字符序列.我怎么能只在XSLT 1.0单个空格替换一系列空白字符?例如"..x.y...\n\t..z."(为了便于阅读,用点替换的空格)应该成为".x.y.z.".
使用此XPath 1.0表达式:
concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
normalize-space(),
substring(' ', 1 + not(substring(., string-length(.)) = ' '))
)
Run Code Online (Sandbox Code Playgroud)
要验证这一点,请进行以下转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select=
"concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
normalize-space(),
substring(' ', 1 + not(substring(., string-length(.)) = ' '))
)
"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
应用于此XML文档时:
<t>
<t1> xxx yyy zzz </t1>
<t2>xxx yyy zzz</t2>
<t3> xxx yyy zzz</t3>
<t4>xxx yyy zzz </t4>
</t>
Run Code Online (Sandbox Code Playgroud)
产生想要的,正确的结果:
<t>
<t1> xxx yyy zzz </t1>
<t2>xxx yyy zzz</t2>
<t3> xxx yyy zzz</t3>
<t4>xxx yyy zzz </t4>
</t>
Run Code Online (Sandbox Code Playgroud)
小智 2
如果没有贝克尔的方法,你可以使用一些沮丧的字符作为标记:
translate(normalize-space(concat('',.,'')),'','')
Run Code Online (Sandbox Code Playgroud)
注意:三个函数调用...
或者使用任何字符但重复某些表达式:
substring(
normalize-space(concat('.',.,'.')),
2,
string-length(normalize-space(concat('.',.,'.'))) - 2
)
Run Code Online (Sandbox Code Playgroud)
在 XSLT 中,您可以轻松声明变量:
<xsl:variable name="vNormalize" select="normalize-space(concat('.',.,'.'))"/>
<xsl:value-of select="susbtring($vNormalize,2,string-length($vNormalize)-2)"/>
Run Code Online (Sandbox Code Playgroud)