gpt4 book ai didi

java - XSLT 扩展 : I am trying to use a custom java function in XSLT but am not able to pin point the mistake

转载 作者:行者123 更新时间:2023-12-02 05:05:54 26 4
gpt4 key购买 nike

我正在尝试使用 java 类中的阶乘函数。我使用的其他两个函数工作正常,但我不知道阶乘函数做错了什么。

下面是 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

最佳答案

使用 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

关于java - XSLT 扩展 : I am trying to use a custom java function in XSLT but am not able to pin point the mistake,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27798008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com