如何在 xsl 中指定要在 saxon 中运行的内联 javascript
我有一个 xslt 转换,在 Altova XML Spy 中运行时工作正常,但在 saxon9he.jar 中出现错误。 请参阅下面的代码片段和撒克逊错误消息。
为了使用 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"/>
我使用的 saxon 调用如下所示:
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()
I have an xslt transformation that works fine when run in Altova XML Spy, but errors in saxon9he.jar.
See below for the code snippets and the saxon error message.
What has to be done differently for this to work using saxon as xslt processor?
<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>
The custom script is called in xpath expressions like the following:
<xsl:value-of select="js:mergeStrings(str1, str2)" disable-output-escaping="yes"/>
The saxon call I use is like the following:
java -jar C:\saxon9he.jar -s:C:\src.xml -xsl:C:\transform.xslt -o:C:\out.html
Saxon now errors with a message similar to the following:
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,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。As far as I am aware Saxon does not support writing extension functions in Javascript. You can use the XSLT 2.0
xsl:function
to write functions with pure XSLT and XPath 2.0 and you can write extension functions (http://www.saxonica.com/documentation/extensibility/intro.xml) in Java for the Java version of Saxon and with .NET for the .NET version of Saxon. But Javascript is not supported I think.