java 如何在xslt文件中调用外部java函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7733300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call external java function in xslt file?
提问by user899628
I have this xslt file that I need to call a java function placed somewhere else in the same application. In the xslt file I have
我有这个 xslt 文件,我需要调用位于同一应用程序中其他位置的 java 函数。在 xslt 文件中,我有
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="java"
xmlns:test_my="vobs.plugins.WikiParser.WikiParser"
version="2.0">
<xsl:output indent="yes" method="html"/>
<xsl:template match="/">
<H1>
<xsl:value-of select="WikiDescription/Title"/>
</H1>
Summary: <xsl:value-of select="WikiDescription/Description"/>
<xsl:variable name="text">
<xsl:value-of select="WikiDescription/Text"/>
</xsl:variable>
<p>
<xsl:value-of select="test_my:parse2($text)"
disable-output-escaping="yes"/>
</p>
but when I try to excute this xlst file I got the following error
但是当我尝试执行此 xlst 文件时,出现以下错误
XSL transform reported error:
XPath syntax error at char 21 on line -1 in {test_my:parse2($text)}:
Cannot find a matching 1-argument function named
{vobs.plugins.WikiParser.WikiParser}parse2()
it seems like that it couldn't find the java class, so what's the right way to do this? some code example will be even better. Thanks in advance!
似乎找不到 java 类,那么正确的方法是什么?一些代码示例会更好。提前致谢!
回答by neilgmacy
I had exactly the same problem and this solved it for me where urn:java:
and java:
both failed.
我遇到了完全相同的问题,这为我解决了问题,urn:java:
并且java:
都失败了。
Assuming you're using Xalan to do the transformation, you should change xmlns:test_my="vobs.plugins.WikiParser.WikiParser"
to xmlns:test_my="xalan://vobs.plugins.WikiParser.WikiParser"
.
假设您使用 Xalan 进行转换,您应该更改xmlns:test_my="vobs.plugins.WikiParser.WikiParser"
为xmlns:test_my="xalan://vobs.plugins.WikiParser.WikiParser"
.
回答by shivamvds
It depends on what XSLT transformation processor you are using. Saxon uses urn:java:
; Xalan uses xalan://
.
这取决于您使用的 XSLT 转换处理器。撒克逊人使用urn:java:
;夏兰使用xalan://
.
回答by Itay Maman
You need to change the namespace declaration (at the xsl:stylesheet element) from xmlns:test_my="vobs.plugins.WikiParser.WikiParser"
to xmlns:test_my="urn:java:vobs.plugins.WikiParser.WikiParser"
您需要将命名空间声明(在 xsl:stylesheet 元素处)从xmlns:test_my="vobs.plugins.WikiParser.WikiParser"
更改为xmlns:test_my="urn:java:vobs.plugins.WikiParser.WikiParser"
The rationale is as follows: In order to use a class C from package a.b you need to define a namespace prefix and associate it with the urn:java:a.b.C
.
基本原理如下:为了使用包 ab 中的类 C,您需要定义一个命名空间前缀并将其与urn:java:a.b.C
.
If I understand XSL correctly the urn:
prefix is needed when importing Java code that is not part of the standard library (as in your case). If you only need to import standard library classes then "java:" will do.
如果我正确理解 XSL,urn:
则在导入不属于标准库的 Java 代码时需要前缀(如您的情况)。如果您只需要导入标准库类,那么“java:”就可以了。
(Further details: http://cafeconleche.org/books/xmljava/chapters/ch17s03.html)
(更多详情:http: //cafeconleche.org/books/xmljava/chapters/ch17s03.html)
[EDIT: change "java:" -> "urn:java:"]
[编辑:更改“java:”->“urn:java:”]