Skip to content

Run Via JAXP

elisasibarani edited this page Jun 14, 2016 · 1 revision

It is recommended to use the JavaWrapper when running Krextor from Java code. However, in certain complex cases you may want additional customization. Here is how to directly run the Krextor XSLT from Java using  JAXP. In this example, we extract RXR from OMDoc; see the list of all input and output formats.

/* DOM */ Document doc;

DOMSource ds = new DOMSource(doc, /* systemId = */ doc.getDocumentURI()); // make sure that the DOM document knows its URI! // setting the system ID seems to be necessary // TODO ask on Saxon list why it is not taken from `doc' DOMResult dr = new DOMResult();

TransformerFactory tf = TransformerFactory.newInstance(); // or the one that you already have String transformerName = Krextor.getTransformerName("omdoc", "rxr"); // result: "xslt/transform-omdoc..rxr.xsl" -- you can alternatively hard-code this URL u = Krextor.class.getResource(transformerName); // if you really don't use any of the Krextor Java classes, you can // alternatively directly put the Krextor XSLTs into your project Transformer t = tf.newTransformer( new StreamSource(u.openStream(), /* systemId = */ u.toExternalForm());

t.transform(ds, dr);

Document rxr_dom = (Document) dr.getNode();

Pre-compiling XSLTs using Templates

TODO

How to obtain a DOM document

Here is an example of how to obtain a DOM document from a file:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
// TODO say something about setting the URI of the document
Clone this wiki locally