我正在尝试使用java类的阶乘函数。我使用的另外两个函数工作正常,但我不知道我对factorial函数做错了什么。
以下是XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:date="java.util.Date" exclude-result-prefixes="#all">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:if test="function-available('date:toString') and function-available('date:new')">
<p><xsl:value-of select="date:toString(date:new())"/></p>
</xsl:if>
<xsl:if xmlns:string="java.lang.String" test="function-available('string:toUpperCase')" exclude-result-prefixes="string">
<p><xsl:value-of select="string:toUpperCase('pune')" /></p>
</xsl:if>
<xsl:if xmlns:fact="java" test="function-available('fact:factorial.calculateFactorial')" exclude-result-prefixes="fact">
<p><xsl:value-of select="fact:factorial.calculateFactorial(5)" /></p>
</xsl:if>
</xsl:template>
以下是java类:
public class factorial{
public int calculateFactorial(int number){
int fact=1;
while(number>=1){
fact = fact * number;
number--;
}
return fact;
}
}
该类和xslt位于同一文件夹中。
错误是:
Error at xsl:value-of on line 13 column 59 of extension-functions.xslt:
XPST0017: XPath syntax error at char 26 on line 13 in {fact:calculateFactorial(5)}:
Cannot find a matching 1-argument function named {urn:java:factorial}calculateFactorial()
Failed to compile stylesheet. 1 error detected.
Saxon版本是:来自Saxonica的Saxon 9.1.0.8J
答案 0 :(得分:2)
使用Saxon 9.6 PE,以下内容适用于我:
<xsl:if xmlns:fact="java:Factorial" test="function-available('fact:new') and function-available('fact:calculateFactorial')" exclude-result-prefixes="fact">
<p><xsl:value-of select="fact:calculateFactorial(fact:new(), 5)" /></p>
</xsl:if>
Java类是
public class Factorial{
public int calculateFactorial(int number){
int fact=1;
while(number>=1){
fact = fact * number;
number--;
}
return fact;
}
}
调用Saxon的命令行是
java.exe -cp 'C:\Program Files (x86)\Saxonica\SaxonPE9.6J\saxon9pe.jar;.' net.sf.saxon.Transform .\test2015010101.xml .\test2015010601.xsl
其中.
包含Factorial.class
。