0

I'm using eXist-db within my java web application (mvc paradigm). I want to retrieve xml data from the database, then transform this xml with a XSLT transformation. Here are the steps I go through.

  1. I query the database and I get an XMLResource, or a ResourceSet as result.

  2. Now I've got to transform the XML I retrieved from the database (as XMLResource) with a XSLT transformation.

    Since XSLT transformations (using javax.xml.transform.Transformer) need a xml Source as parameter, I've got to convert from XMLResource to StreamSource.

But is this the best way to handle an xml resource? Should I convert my XMLResource in something else instead?

1 Answer 1

1

XMLResource provides a getContentAsSAX method to feed the XML directly to a SAX ContentHandler. And javax.xml.transform provides the concept of a TransformerHandler, exposing a Transformer as a SAX ContentHandler. So you can use these two in combination:

Source stylesheet = ... // however you're loading your stylesheet
TransformerHandler handler = ((SAXTransformerFactory)transformerFactory)
                              .newTransformerHandler(stylesheet);
Result result = ... // StreamResult, DOMResult etc.
handler.setResult(result);
xmlResource.getContentAsSAX(handler);

// result now holds the result of the transformation

For the reverse direction, if you need an XMLResource to act as the target of a transformation, then you can use SAXResult with a normal Transformer

Source stylesheet = ... // however you're loading your stylesheet
Transformer transformer = transformerFactory.newTransformer(stylesheet);
Source source = ... // StreamSource, DOMSource, etc.
Result result = new SAXResult(xmlResource.setContentAsSAX());
transformer.transform(source, result);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.