无法从 SaxonEE9-9-1-4J 中的 XSLT 调用扩展 Java 方法
Cannot invoke extension Java method from XSLT in SaxonEE9-9-1-4J
我有以下简单的 XSLT 示例:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:eg="http://example.com/saxon-extension"
xmlns:eks="ekstern"
xmlns:dmp="http://arealinformation.miljoeportal.dk/gis/services/distribution/MapServer/WFSServer"
exclude-result-prefixes="eg">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:copy>
<xsl:sequence select="eg:shift-left(1,2)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我有这个基本的 "ShiftLeft" class,这是我从撒克逊人的例子中得到的:https://www.saxonica.com/html/documentation/extensibility/integratedfunctions/ext-full-J.html
package diadem.base.plugin.helpers;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.IntegerValue;
import net.sf.saxon.value.Int64Value;
public class ShiftLeft extends ExtensionFunctionDefinition {
@Override
public StructuredQName getFunctionQName() {
return new StructuredQName("eg", "http://example.com/saxon-extension", "shift-left");
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.SINGLE_INTEGER, SequenceType.SINGLE_INTEGER};
}
@Override
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.SINGLE_INTEGER;
}
@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
@Override
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
long v0 = ((IntegerValue)arguments[0]).longValue();
long v1 = ((IntegerValue)arguments[1]).longValue();
long result = v0<<v1;
return Int64Value.makeIntegerValue(result);
}
};
}
}
出于某种原因,我在 XSLT 中遇到以下错误消息:
Static error in {eg:shift-left(1,2)} in expression in xsl:sequence/@select on line 12 column 56 of conflictsTestTransform.xslt:
XPST0017: Cannot find a 2-argument function named Q{http://example.com/saxon-extension}shift-left()
我真的不知道为什么它不能调用 Java 扩展函数中的 "Call" 方法。我用于转换的代码:
private static Document transform(Document inputDoc, File xsltFile) throws XSLTransformException {
JDOMSource source = new JDOMSource(inputDoc, null)
JDOMResult result = new JDOMResult()
try {
TransformerFactory tFactory = TransformerFactory.newInstance("com.saxonica.config.EnterpriseTransformerFactory", null)
tFactory.setURIResolver(new XsltUriResolver())
if(tFactory instanceof TransformerFactoryImpl) {
TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) tFactory;
Configuration saxonConfig = tFactoryImpl.getConfiguration();
saxonConfig.registerExtensionFunction(new ShiftLeft());
}
Templates templates = TransformerFactory.newInstance("com.saxonica.config.EnterpriseTransformerFactory", null).newTemplates(new StreamSource(xsltFile))
Transformer transformer = templates.newTransformer()
result.setFactory(null) // null ok
try {
transformer.transform(source, result)
return result.getDocument()
}
catch (TransformerException e) {
throw new XSLTransformException("Could not perform transformation", e)
}
} catch (TransformerConfigurationException tce) {
// Error generated by the parser
// log.warn("Transformer Factory error", tce)
} catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
// log.warn("SAXException", sxe)
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
// log.warn("Parser build error", pce)
} catch (IOException ioe) {
// I/O error
// log.warn("IO error", ioe)
}
return null
}
您有两个 TransformerFactories 和两个 Saxon 配置;你已经在其中一个注册了扩展功能,而你正试图在另一个中使用它。
我有以下简单的 XSLT 示例:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:eg="http://example.com/saxon-extension"
xmlns:eks="ekstern"
xmlns:dmp="http://arealinformation.miljoeportal.dk/gis/services/distribution/MapServer/WFSServer"
exclude-result-prefixes="eg">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:copy>
<xsl:sequence select="eg:shift-left(1,2)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我有这个基本的 "ShiftLeft" class,这是我从撒克逊人的例子中得到的:https://www.saxonica.com/html/documentation/extensibility/integratedfunctions/ext-full-J.html
package diadem.base.plugin.helpers;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.IntegerValue;
import net.sf.saxon.value.Int64Value;
public class ShiftLeft extends ExtensionFunctionDefinition {
@Override
public StructuredQName getFunctionQName() {
return new StructuredQName("eg", "http://example.com/saxon-extension", "shift-left");
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.SINGLE_INTEGER, SequenceType.SINGLE_INTEGER};
}
@Override
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.SINGLE_INTEGER;
}
@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
@Override
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
long v0 = ((IntegerValue)arguments[0]).longValue();
long v1 = ((IntegerValue)arguments[1]).longValue();
long result = v0<<v1;
return Int64Value.makeIntegerValue(result);
}
};
}
}
出于某种原因,我在 XSLT 中遇到以下错误消息:
Static error in {eg:shift-left(1,2)} in expression in xsl:sequence/@select on line 12 column 56 of conflictsTestTransform.xslt:
XPST0017: Cannot find a 2-argument function named Q{http://example.com/saxon-extension}shift-left()
我真的不知道为什么它不能调用 Java 扩展函数中的 "Call" 方法。我用于转换的代码:
private static Document transform(Document inputDoc, File xsltFile) throws XSLTransformException {
JDOMSource source = new JDOMSource(inputDoc, null)
JDOMResult result = new JDOMResult()
try {
TransformerFactory tFactory = TransformerFactory.newInstance("com.saxonica.config.EnterpriseTransformerFactory", null)
tFactory.setURIResolver(new XsltUriResolver())
if(tFactory instanceof TransformerFactoryImpl) {
TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) tFactory;
Configuration saxonConfig = tFactoryImpl.getConfiguration();
saxonConfig.registerExtensionFunction(new ShiftLeft());
}
Templates templates = TransformerFactory.newInstance("com.saxonica.config.EnterpriseTransformerFactory", null).newTemplates(new StreamSource(xsltFile))
Transformer transformer = templates.newTransformer()
result.setFactory(null) // null ok
try {
transformer.transform(source, result)
return result.getDocument()
}
catch (TransformerException e) {
throw new XSLTransformException("Could not perform transformation", e)
}
} catch (TransformerConfigurationException tce) {
// Error generated by the parser
// log.warn("Transformer Factory error", tce)
} catch (SAXException sxe) {
// Error generated by this application
// (or a parser-initialization error)
// log.warn("SAXException", sxe)
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
// log.warn("Parser build error", pce)
} catch (IOException ioe) {
// I/O error
// log.warn("IO error", ioe)
}
return null
}
您有两个 TransformerFactories 和两个 Saxon 配置;你已经在其中一个注册了扩展功能,而你正试图在另一个中使用它。