我正在使用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).我做错了什么?
发布于 2011-12-31 02:51:01
XSLT 1.0中没有对正则表达式的内置支持。您正在调用的EXSLT函数是扩展函数的第三方规范,这些扩展函数可能在某些处理器中可用;您得到的错误消息表明它不适用于您的特定处理器(或者您需要以某种方式为该处理器安装/配置它)。
您正在使用Java,所以以Saxon的形式使用XSLT2.0应该没有任何障碍。
发布于 2011-12-30 19:39:03
使用fn:replace(string,pattern,replace)本身怎么样?我不知道这在XSLT1.0中是否也可用;请检查。
可以在here中找到string.replace函数的示例
根据上述链接中的文档,replace采用正则表达式模式。
函数的fn:replace替换字符串中与正则表达式匹配的部分。所使用的正则表达式语法由XML Schema定义,但在XQueryXPath/XSLT中做了一些修改/添加。$pattern参数是一个正则表达式。虽然拥有正则表达式的功能很好,但如果您只是想替换特定的字符序列,则不必熟悉正则表达式即可实现;只要不包含任何特殊字符,您只需指定要替换为$pattern的字符串即可。
因此您可以使用fn:replace(text(),'0-9{1,4}s:','')
https://stackoverflow.com/questions/8678077
复制相似问题