Saxon-HE Integrated Extension Functions | how and where?

前端 未结 1 550
南旧
南旧 2021-01-05 19:32

Although writing Saxon Integrated Extension Functions are pretty clear to me.

I have red:

  • http://www.saxonica.com/html/documentation/extensibility/in
1条回答
  •  囚心锁ツ
    2021-01-05 19:52

    TestMain.java - some java file with access to transformation factory

    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 - the actual extension function logic

    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. file

    
    
        
    
        
            
                    
            
         
    
    

    result.xml - the result of entire process

    
    Saxon is being extended correctly.
    

    Please notice that namespace used in .java and .xsl file must be the same, declared in QName

    0 讨论(0)
提交回复
热议问题