尽管编写Saxon集成扩展函数对我来说非常清楚。
我有红色:
我很难找到如何让他们真正工作的信息。
问:放置文件的位置,我是否必须编译任何内容,是否必须编辑saxon配置?基本上除了使用s9api处理器注册扩展功能外,我还需要做些什么呢?
答案 0 :(得分:4)
TestMain.java - 一些可以访问转换工厂的java文件
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import net.sf.saxon.TransformerFactoryImpl;
import net.sf.saxon.s9api.ExtensionFunction;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.jaxp.SaxonTransformerFactory;
import location.to.test.java.file.Test;
public class TestMain {
public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
// Grab the handle of Transformer factory and cast it to TransformerFactoryImpl
TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) factory;
// Get the currently used processor
net.sf.saxon.Configuration saxonConfig = tFactoryImpl.getConfiguration();
Processor processor = (Processor) saxonConfig.getProcessor();
// Here extension happens, test comes from class Test -> Test.java
ExtensionFunction test = new Test();
processor.registerExtensionFunction(test);
Source xslt = new StreamSource(new File("test.xsl"));
Transformer transformer = factory.newTransformer(xslt);
Source text = new StreamSource(new File("input.xml"));
transformer.transform(text, new StreamResult(new File("result.xml")));
}
}
Test.java - 实际的扩展函数逻辑
import net.sf.saxon.s9api.ExtensionFunction;
import net.sf.saxon.s9api.ItemType;
import net.sf.saxon.s9api.OccurrenceIndicator;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.SequenceType;
import net.sf.saxon.s9api.XdmAtomicValue;
import net.sf.saxon.s9api.XdmValue;
public class Test implements ExtensionFunction {
@Override
public QName getName() {
return new QName("http://some.namespace.com", "test");
}
@Override
public SequenceType getResultType() {
return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
}
@Override
public net.sf.saxon.s9api.SequenceType[] getArgumentTypes() {
return new SequenceType[] {};
}
@Override
public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
String result = "Saxon is being extended correctly.";
return new XdmAtomicValue(result);
}
}
test.xsl - 测试xsl。文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://some.namespace.com">
<xsl:output indent="yes"/>
<xsl:template match="/">
<root>
<xsl:value-of select="ext:test()" />
</root>
</xsl:template>
</xsl:stylesheet>
result.xml - 整个过程的结果
<?xml version="1.0" encoding="UTF-8"?>
<root>Saxon is being extended correctly.</root>
请注意.java和.xsl文件中使用的命名空间必须相同,在QName中声明