0

我正在使用 xslt 1.0 来转换我的 xml。

我有这个字符串:

hello 1s: This is very nice day. 9s: Christmas is about to come 14s: and christmas preparation is just on 25s: this is awesome!! 

我想像这样格式化它:

hello This is very nice day. Christmas is about to come and christmas preparation is just on this is awesome!! 

为此,我尝试了这个 xslt:

<?xml version='1.0' encoding='UTF-8' ?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:regexp="http://exslt.org/regular-expressions"
                extension-element-prefixes="regexp"  >
<xsl:import href="regexp.xsl" />
  <xsl:template match='/'>
         <xsl:value-of select="regexp:replace(string(.), '[0-9]{1,4}s: ', 'g', '')" />
  </xsl:template>
</xsl:stylesheet>

但是当我运行它时出现以下错误:

java.lang.NoSuchMethodException: For extension function, could not find method java.lang.String.replace([ExpressionContext,] #STRING, #STRING, #STRING).

我究竟做错了什么?

4

2 回答 2

1

XSLT 1.0 中没有对正则表达式的内置支持。您调用的 EXSLT 函数是扩展函数的第三方规范,可能在某些处理器中可用;您收到的错误消息表明它不适用于您的特定处理器(或者您需要以某种方式为该处理器安装/配置它)。

您正在使用 Java,因此使用 Saxon 形式的 XSLT 2.0 应该没有障碍。

于 2011-12-30T18:51:01.057 回答
0

如何使用fn:replace(string,pattern,replace)本身。我不知道这在 XSLT1.0 中是否也可用;请检查。

string.replace 函数的示例可以在这里找到

根据上述链接中的文档,替换采用正则表达式模式。

fn:replace 函数替换匹配正则表达式的字符串部分。使用的正则表达式语法由 XML Schema 定义,并在 XQueryXPath/XSLT 中进行了一些修改/添加。$pattern 参数是一个正则表达式。虽然拥有正则表达式的功能很不错,但如果您只是想替换特定的字符序列,您不必熟悉正则表达式即可。只要不包含任何特殊字符,您就可以为 $pattern 指定要替换的字符串。

因此您可以使用 fn:replace(text(), '[0-9]{1,4}s: ', '')

于 2011-12-30T11:39:03.393 回答