如何用一个空格替换空格序列但不在 XSLT 中修剪?
问题描述:
函数 normalize-space
删除前导和尾随空格,并用单个空格替换空格字符序列.如何在 XSLT 1.0 中仅用单个空格替换空白字符序列?例如,"..xy..\n\t..z."
(空格替换为一个点以提高可读性)应该变成 ".xyz"
.
The function normalize-space
removes leading and trailing whitespace and replaces sequences of whitespace characters by a single space. How can I only replaces sequences of whitespace characters by a single space in XSLT 1.0? For instance "..x.y...\n\t..z."
(spaces replaced by a dot for readability) should become ".x.y.z."
.
答
如果没有 Becker 的方法,您可以使用一些 不鼓励 字符作为标记:
Without Becker's method, you could use some discouraged character as mark:
translate(normalize-space(concat('',.,'')),'','')
注意:三个函数调用...
或者使用任何字符但重复某些表达式:
Or with any character but repeating some expression:
substring(
normalize-space(concat('.',.,'.')),
2,
string-length(normalize-space(concat('.',.,'.'))) - 2
)
在 XSLT 中,您可以轻松地声明一个变量:
In XSLT you can easily declare a variable:
<xsl:variable name="vNormalize" select="normalize-space(concat('.',.,'.'))"/>
<xsl:value-of select="susbtring($vNormalize,2,string-length($vNormalize)-2)"/>