我有一个 xslt 转换,在 Altova XML Spy 中运行时工作正常,但在 saxon9he.jar 中出现错误。 请参阅下面的代码片段和 saxon 错误消息。
要使用 saxon 作为 xslt 处理器,必须采取哪些不同的做法?
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:custom="urn:schemas-microsoft-com:xslt"
xmlns:js="urn:custom-javascript"
exclude-result-prefixes="custom js">
<custom:script language="Javascript" implements-prefix="js">
<![CDATA[
function mergeStrings (string1, string2) {
... java code ...
}
]]>
</custom:script>
自定义脚本在 xpath 表达式中调用,如下所示:
<xsl:value-of select="js:mergeStrings(str1, str2)" disable-output-escaping="yes"/>
我使用的撒克逊调用如下:
java -jar C:\saxon9he.jar -s:C:\src.xml -xsl:C:\transform.xslt -o:C:\out.html
Saxon 现在出现错误,并显示类似于以下内容的消息:
Error at xsl:value-of on line 775 column 105 of transform.xslt:
XPST0017: XPath syntax error at char 43 on line 775 in {...gs(str1, str2...}:
Cannot find a matching 2-argument function named {urn:custom-javascript}mergeStrings()
最佳答案
据我所知,Saxon 不支持在 Javascript 中编写扩展函数。您可以使用 XSLT 2.0 xsl:function
来使用纯 XSLT 和 XPath 2.0 编写函数,并且可以编写扩展函数 (http://www.saxonica.com/documentation/extensibility/intro.xml)使用 Java 表示 Java 版本的 Saxon,使用 .NET 表示 .NET 版本的 Saxon。但我认为 Javascript 不受支持。
关于javascript - 如何在 xsl 中指定要在 saxon 中运行的内联 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9035216/