TestMain.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一些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 - test 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 - 整個過程的結果
請注意到的.java和的.xsl文件中使用的必須是該命名空間同樣,在QName中聲明
http://www.saxonica.com/html/documentation/extensibility/integratedfunctions/ext-simple-J.html中的示例不適用於您嗎?我將這些代碼粘貼到使用NetBeans創建的Java應用程序模板中,並將'saxon9he.jar'添加爲庫,讓IDE爲所使用的類和接口修復導入部分(我必須修復一個用於'QName'的導入部分因爲選擇的導入用於不同的類),讓IDE向'main'方法添加'拋出SaxonApiException',編譯並運行代碼並添加'System.out.println(result);'輸出結果。 –
還是你想實現你的擴展函數,並使用它們從命令行運行'net.sf.saxon.Transform'? –
** 1)**感謝您快速回復Martin。同時,我不得不弄清這樣一個基本的東西,比如下載沒有編譯過的'SaxonHE'版本,正如你所提到的,我試圖在'Eclipse'中用這個源代碼創建項目。它在那裏,但老實說,我不知道從哪裏開始。所以我的問題更多的是關於用'SaxonHE'源代碼設置'Java'環境。 ** 2)**我希望在'xsl模板'中使用我的擴展函數,例如'' –