如何在 xslt 中修剪? [英] How to Trim in xslt?

查看:34
本文介绍了如何在 xslt 中修剪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想左右修剪空白在:
<xsl:value-of select="Datas/Data[@key='Name']/string"/>

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

我该怎么做?

推荐答案

最简单的方法就是使用FXSL.

The easiest way is to use the trim template function of FXSL.

这种转变:

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

trim 的工作原理:

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.

FXSL 的 trim 函数/模板通过使用 FXSL 的另一个模板/函数来反转字符串来实现这一点.

The FXSL's trim function/template achieves this by using another template/function of FXSL for reversing a string.

所以,处理过程如下:

  1. 消除前导空格.

  1. Eliminate leading white-space.

反转结果.

消除前导空格.

终于反转了.

可以查看 FXSL 2.0(用于 XSLT 2.0)中 trim() 的完整代码 此处.它几乎与 FXSL 1.0(用于 XSLT 1.0)的 trim 模板的代码相同.

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

这篇关于如何在 xslt 中修剪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆