如何在xslt中修剪?

发布于 2024-10-06 21:17:31 字数 121 浏览 0 评论 0原文

我想修剪左右空白 在:

我该怎么做?

I want to trim whitespace left and right
in :
<xsl:value-of select="Datas/Data[@key='Name']/string"/>

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妥活 2024-10-13 21:17:31

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.

倾城花音 2024-10-13 21:17:31

最简单的方法是使用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>

应用于此 XML 文档时

<someText>

   This is    some text   

</someText>

产生所需的正确结果

'This is    some text'

如何修剪有效:

消除字符串中所有起始空白字符很容易,但困难的是消除所有结束空白字符。

FXSL 的 trim 函数/模板通过使用 FXSL 的另一个模板/函数来反转字符串来实现此目的。

因此,处理过程如下

  1. 消除前导空白。

  2. 反转结果。

  3. 消除前导空格。

  4. 终于反转了。

可以查看 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:

<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>

when applied on this XML document:

<someText>

   This is    some text   

</someText>

produces the wanted, correct result:

'This is    some text'

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:

  1. Eliminate leading white-space.

  2. Reverse the result.

  3. Eliminate leading whitespace.

  4. 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 the trim template of FXSL 1.0 (for XSLT 1.0).

他夏了夏天 2024-10-13 21:17:31

提供我在 XSLT 2.0 中使用的另一种解决方案,因为它更加简洁和准确(规范化空间不是修剪)。

使用替换函数和正则表达式来获取减去前后空格的内部内容。

replace(Datas/Data[@key='Name']/string,'^\s*(.+?)\s*
, '$1')

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.

replace(Datas/Data[@key='Name']/string,'^\s*(.+?)\s*
, '$1')
月依秋水 2024-10-13 21:17:31

将 @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 示例:

  <xsl:value-of 
       select="php:functionString( 'trim', Datas/Data[@key='Name']/string )"
  />

注意(已编辑):对于非 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:

  <xsl:value-of 
       select="php:functionString( 'trim', Datas/Data[@key='Name']/string )"
  />

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.

靖瑶 2024-10-13 21:17:31

您可以仅使用 XSLT 1.0 来制作自己的:

如果只需要 trim,您可以像下面的模板一样自己实现它。它递归地调用自身并删除字符,直到内置 XSLT 函数 normalize-space() 的第一个和最后一个字符与剩余字符串的第一个和最后一个字符匹配。这两种情况基本上是左修剪和右修剪,首先进行左修剪(外部 otherwise 情况)。

在大型数据集上它可能有点慢,但它工作得很好:

<xsl:template name="Trim">
<xsl:param name="value"/>
<xsl:choose>
  <xsl:when test="string-length($value)=0 or string-length(normalize-space($value))=0">
    <xsl:value-of select="$value"/>
  </xsl:when>
  <xsl:when test="starts-with($value,substring(normalize-space($value),1,1))">
  <xsl:choose>
    <xsl:when test="starts-with(substring($value,string-length($value)-1,1),substring(normalize-space($value),string-length(normalize-space($value))-1,1))">
    <xsl:value-of select="$value"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:call-template name="Trim">
    <xsl:with-param name="value" select="substring($value,1,string-length($value)-1)"/>
    </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  </xsl:when>
  <xsl:otherwise>
  <xsl:call-template name="Trim">
    <xsl:with-param name="value" select="substring($value,2)"/>
  </xsl:call-template>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>

用法:

<xsl:call-template name="Trim"><xsl:with-param name="value" select="mynodehere"/></xsl:call-template>

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 function normalize-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 outer otherwise case).

It might be a bit slow on large datasets but it works fine:

<xsl:template name="Trim">
<xsl:param name="value"/>
<xsl:choose>
  <xsl:when test="string-length($value)=0 or string-length(normalize-space($value))=0">
    <xsl:value-of select="$value"/>
  </xsl:when>
  <xsl:when test="starts-with($value,substring(normalize-space($value),1,1))">
  <xsl:choose>
    <xsl:when test="starts-with(substring($value,string-length($value)-1,1),substring(normalize-space($value),string-length(normalize-space($value))-1,1))">
    <xsl:value-of select="$value"/>
    </xsl:when>
    <xsl:otherwise>
    <xsl:call-template name="Trim">
    <xsl:with-param name="value" select="substring($value,1,string-length($value)-1)"/>
    </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  </xsl:when>
  <xsl:otherwise>
  <xsl:call-template name="Trim">
    <xsl:with-param name="value" select="substring($value,2)"/>
  </xsl:call-template>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>

Usage:

<xsl:call-template name="Trim"><xsl:with-param name="value" select="mynodehere"/></xsl:call-template>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文