如何在xslt中修剪?
我想修剪左右空白 在:
我该怎么做?
I want to trim whitespace left and right
in :<xsl:value-of select="Datas/Data[@key='Name']/string"/>
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
normalize-space(Datas/Data[@key='Name']/string)
可能就足够了,它将修剪空白以及开头和结尾。然而,它也会将之间的任何空白折叠成一个空格,我不知道你是否想要这样。normalize-space(Datas/Data[@key='Name']/string)
might suffice, it will trim white space and the start and end. It does however also collapse any white space in between to a single space, I don't know whether you want that.最简单的方法是使用FXSL的
trim
模板功能。此转换:
应用于此 XML 文档时:
产生所需的正确结果:
如何
修剪
有效:消除字符串中所有起始空白字符很容易,但困难的是消除所有结束空白字符。
FXSL 的
trim
函数/模板通过使用 FXSL 的另一个模板/函数来反转字符串来实现此目的。因此,处理过程如下:
消除前导空白。
反转结果。
消除前导空格。
终于反转了。
可以查看 FXSL 2.0(针对 XSLT 2.0)中
trim()
的完整代码 此处。它与 FXSL 1.0(针对 XSLT 1.0)的trim
模板的代码几乎相同。The easiest way is to use the
trim
template function of FXSL.This transformation:
when applied on this XML document:
produces the wanted, correct result:
How
trim
works:It is easy to eliminate all starting whitespace characters in a string, but the difficult part is to eliminate all ending whitespace characters.
The FXSL's
trim
function/template achieves this by using another template/function of FXSL for reversing a string.So, the processing goes like this:
Eliminate leading white-space.
Reverse the result.
Eliminate leading whitespace.
Finally reverse.
The full code for
trim()
in FXSL 2.0 (for XSLT 2.0) can be seen here. It is almost the same as the code for thetrim
template of FXSL 1.0 (for XSLT 1.0).提供我在 XSLT 2.0 中使用的另一种解决方案,因为它更加简洁和准确(规范化空间不是修剪)。
使用替换函数和正则表达式来获取减去前后空格的内部内容。
Offering another solution that I use in XSLT 2.0 since it's more concise and accurate (normalize-space is not a trim).
Use the replace function and a regular expression to grab the inner content minus the preceding and trailing whitespace.
将 @ricosrealm 针对 XSLT2 用户的解决方案与 @Dimitre 针对 XSLT1 的解决方案进行比较(并检查 FXSL-trim 的行数)。
正则表达式替换功能非常简单,花费更少的CPU时间和程序员的时间。
对于2013年的XSLT1用户
如果您是XSLT1用户,可能是因为您没有选择使用XSLT2。示例:PHP 依赖于 LibXML2 实现,该实现停止于 1999 年标准,未实现 2007 年标准。如今(2013 年),只有一小部分 XSLT1 用户出于性能考虑而这样做。
因此,如果您认为自己被 XSLT1 和框架所困,那么是时候了解并使用“注册函数”了,就像在 PHP 上一样(但任何其他使用 LibXML2 的 Python 或 Javascript 都可以使用 LibXML2 的扩展),以近似 XSLT2 自由/功能。
请参阅适用于您的语言的 XSLTProcessor::registerPHPFunctions。
PHP 示例:
注意(已编辑):对于非 libXML2 实现,例如在 Microsoft (.NET) 框架中,如 @ChristopheDebove(下面的评论)所示,也有针对注册函数的解决方案。当然,对于 Java,还有 SAXON,它是当今 XSLT2 唯一好的开源软件。
...如果有一天(请参阅有关何时此处和此处)您可以在同一框架(例如 PHP)中用 XSLT2 替换 XSLT1,无需更改 XSLT 脚本,
因为预计注册的函数将是相同的。
Compare the @ricosrealm's solution, for XSLT2 users, with the @Dimitre's for XSLT1 (and check also the number of lines of the FXSL-trim).
The regular expression replace function is so simple, spend less CPU-time and less programmer's time.
For XSLT1 users in 2013
If you is a XSLT1 user, is probably because you not have choice to use XSLT2. Example: PHP depends on LibXML2 implementatin, that stoped in the 1999 standard, not implements the 2007's standard. Today (2013) only a fraction of XSLT1 users do this by performance considerations.
So, if you assumes that you is trapped by XSLT1 and your framework, is time to know and to use "registered functions", like on PHP (but any other like Python or Javascript using LibXML2 can use LibXML2's extensions), to approximate to XSLT2 liberty/functionality.
See XSLTProcessor::registerPHPFunctions on your language.
PHP example:
NOTE (edited): for non-libXML2 implementations, like at Microsoft (.NET) frameworks, as showed by @ChristopheDebove (comments below), there are also solutions for registered functions. Of course, for Java there are SAXON, the only good open source of XSLT2 today.
... If one day (see opinions about when here and here) you replace XSLT1 by XSLT2 in the same framework (ex. PHP), you not need to change your XSLT scripts,
because is expected that registred functions will be the same.
您可以仅使用 XSLT 1.0 来制作自己的:
如果只需要
trim
,您可以像下面的模板一样自己实现它。它递归地调用自身并删除字符,直到内置 XSLT 函数normalize-space()
的第一个和最后一个字符与剩余字符串的第一个和最后一个字符匹配。这两种情况基本上是左修剪和右修剪,首先进行左修剪(外部otherwise
情况)。在大型数据集上它可能有点慢,但它工作得很好:
用法:
You can make your own using only XSLT 1.0:
If only a
trim
is required, you can just implement it yourself like the following template. It calls itself recursively and strips away chars until the first and last char of the built-in XSLT functionnormalize-space()
match the first and last chars of the remaining string. The two cases are basically left-trim and right-trim, with left-trim being done first (the outerotherwise
case).It might be a bit slow on large datasets but it works fine:
Usage: