Java 在内置模板规则中找不到名为{NAME}METHOD()的匹配0参数函数
我注册了没有参数的ExtensionFunctionDefinition,但无法调用它。Java 在内置模板规则中找不到名为{NAME}METHOD()的匹配0参数函数,java,xml,xslt,sax,saxon,Java,Xml,Xslt,Sax,Saxon,我注册了没有参数的ExtensionFunctionDefinition,但无法调用它。 有什么问题,如何解决? 看起来函数未注册。 代码如下: 撒克逊人 XSLT 变压器 Processor processor = new Processor(false); Configuration configuration = new Configuration(); TransformerFactoryImpl transformerFactory = new Transfor
有什么问题,如何解决?
看起来函数未注册。
代码如下: 撒克逊人 XSLT 变压器
Processor processor = new Processor(false);
Configuration configuration = new Configuration();
TransformerFactoryImpl transformerFactory = new TransformerFactoryImpl();
processor.registerExtensionFunction(new DateExtensionFunction());
configuration.setProcessor(processor);
transformerFactory.setConfiguration(configuration);
//...newTransformer
处理器、配置和TransformerFactory之间的关系错误 最好将配置视为保存所有重要数据,将处理器和TransformerFactory视为配置顶部的API贴面 创建处理器时,它会在下面创建自己的配置。TransformerFactoryImpl也是如此。这里有三个配置对象,两个是Saxon创建的,另一个是您创建的。扩展函数是在支持(s9api)处理器的配置中注册的,它与您在JAXP TransformerFactory中使用的处理器没有关系 我建议您使用JAXP或s9api,但避免将它们混合使用。如果要使用JAXP,请执行以下操作:
TransformerFactoryImpl transformerFactory = new TransformerFactoryImpl();
Configuration config = transformerFactory.getConfiguration();
config.registerExtensionFunction(new DateExtensionFunction());
请注意,在Saxon 9.7中,JAXP接口是作为s9api接口之上的一个层实现的。下面是一些有效的代码(在Saxon 9.7 HE下测试)。我不知道为什么你的没有:请把一个完整的程序放在一起,说明这个问题
import ....;
public class ExtensionTest extends TestCase {
public class DateExtensionFunction extends ExtensionFunctionDefinition {
public StructuredQName getFunctionQName() {
return new StructuredQName("", "http://date.com", "getFormattedNow");
}
public net.sf.saxon.value.SequenceType[] getArgumentTypes() {
return new net.sf.saxon.value.SequenceType[]{net.sf.saxon.value.SequenceType.OPTIONAL_STRING};
}
public net.sf.saxon.value.SequenceType getResultType(net.sf.saxon.value.SequenceType[] sequenceTypes) {
return net.sf.saxon.value.SequenceType.SINGLE_STRING;
}
public boolean trustResultType() {
return true;
}
public int getMinimumNumberOfArguments() {
return 0;
}
public int getMaximumNumberOfArguments() {
return 1;
}
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
return new StringValue("TEST");
}
};
}
}
public void testIntrinsicExtension() {
try {
TransformerFactoryImpl factory = new TransformerFactoryImpl();
factory.getConfiguration().registerExtensionFunction(new DateExtensionFunction());
String xsl = "<e xsl:version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " +
"result='{Q{http://date.com}getFormattedNow()}'/>";
Templates t = factory.newTemplates(new StreamSource(new StringReader(xsl)));
StringWriter sw = new StringWriter();
t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), new StreamResult(sw));
System.err.println(sw.toString());
} catch (TransformerConfigurationException tce) {
tce.printStackTrace();
fail();
} catch (TransformerException e) {
e.printStackTrace();
fail();
}
}
}
导入。。。。;
公共类ExtensionTest扩展了TestCase{
公共类DateExtensionFunction扩展了ExtensionFunctionDefinition{
公共结构QName getFunctionQName(){
返回新的StructuredQName(“,”http://date.com“,“getFormattedNow”);
}
public net.sf.saxon.value.SequenceType[]getArgumentTypes(){
返回新的net.sf.saxon.value.SequenceType[]{net.sf.saxon.value.SequenceType.OPTIONAL_STRING};
}
public net.sf.saxon.value.SequenceType getResultType(net.sf.saxon.value.SequenceType[]SequenceType){
返回net.sf.saxon.value.SequenceType.SINGLE_字符串;
}
公共布尔信任结果类型(){
返回true;
}
public int getMinimumNumberOfArguments(){
返回0;
}
public int getMaximumNumberOfArguments(){
返回1;
}
公共扩展函数调用makeCallExpression(){
返回新的ExtensionFunctionCall(){
公共序列调用(XPathContext上下文,序列[]参数)抛出XPathException{
返回新的字符串值(“测试”);
}
};
}
}
公共无效TestInResistenceExtension(){
试一试{
TransformerFactoryImpl工厂=新TransformerFactoryImpl();
factory.getConfiguration().registerExtensionFunction(新的DateExtensionFunction());
字符串xsl=”“;
模板t=factory.newTemplates(新StreamSource(新StringReader(xsl));
StringWriter sw=新的StringWriter();
t、 newTransformer().transform(新的StreamSource(新的StringReader)(“”),新的StreamResult(sw));
System.err.println(sw.toString());
}捕获(TransformerConfiguration异常tce){
tce.printStackTrace();
失败();
}捕获(转换异常e){
e、 printStackTrace();
失败();
}
}
}
输出为:
<?xml version="1.0" encoding="UTF-8"?><e result="TEST"/>
解决方案(仅适用于Saxon 9.4):
非常感谢你!但它仍然没有看到函数:TransformerFactoryImpl transformerFactory=new TransformerFactoryImpl();transformerFactory.getConfiguration().registerExtensionFunction(新的DateExtensionFunctionDefinition());
Processor processor = new Processor(false);
Configuration configuration = new Configuration();
TransformerFactoryImpl transformerFactory = new TransformerFactoryImpl();
processor.registerExtensionFunction(new DateExtensionFunction());
configuration.setProcessor(processor);
transformerFactory.setConfiguration(configuration);
//...newTransformer
TransformerFactoryImpl transformerFactory = new TransformerFactoryImpl();
Configuration config = transformerFactory.getConfiguration();
config.registerExtensionFunction(new DateExtensionFunction());
import ....;
public class ExtensionTest extends TestCase {
public class DateExtensionFunction extends ExtensionFunctionDefinition {
public StructuredQName getFunctionQName() {
return new StructuredQName("", "http://date.com", "getFormattedNow");
}
public net.sf.saxon.value.SequenceType[] getArgumentTypes() {
return new net.sf.saxon.value.SequenceType[]{net.sf.saxon.value.SequenceType.OPTIONAL_STRING};
}
public net.sf.saxon.value.SequenceType getResultType(net.sf.saxon.value.SequenceType[] sequenceTypes) {
return net.sf.saxon.value.SequenceType.SINGLE_STRING;
}
public boolean trustResultType() {
return true;
}
public int getMinimumNumberOfArguments() {
return 0;
}
public int getMaximumNumberOfArguments() {
return 1;
}
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
return new StringValue("TEST");
}
};
}
}
public void testIntrinsicExtension() {
try {
TransformerFactoryImpl factory = new TransformerFactoryImpl();
factory.getConfiguration().registerExtensionFunction(new DateExtensionFunction());
String xsl = "<e xsl:version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " +
"result='{Q{http://date.com}getFormattedNow()}'/>";
Templates t = factory.newTemplates(new StreamSource(new StringReader(xsl)));
StringWriter sw = new StringWriter();
t.newTransformer().transform(new StreamSource(new StringReader("<a/>")), new StreamResult(sw));
System.err.println(sw.toString());
} catch (TransformerConfigurationException tce) {
tce.printStackTrace();
fail();
} catch (TransformerException e) {
e.printStackTrace();
fail();
}
}
}
<?xml version="1.0" encoding="UTF-8"?><e result="TEST"/>
@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
@Override
@SuppressWarnings("unchecked")
public SequenceIterator call(SequenceIterator[] arguments, XPathContext context) throws XPathException {
return SingletonIterator.makeIterator(StringValue.makeStringValue("TEST"));
}
};
}