How to validate XSLT output in java?

D

Damon Rand

Howdi,

This should be simple but I am lost in the maze of java xml/xsl
classes. Given a dom Document and an xsl Templates object I want to
run the document through a transformer and validate the output against
a schema and then return the output xml as another Document.

At the moment the only way I can work out how to do this is by
writing the xslt output to a File, then parsing it back in from the
File with validation enabled.. How do I do it directly? Here is where
I have got too. I am working with Java 1.4.2 default implementation.


Regards,
Damon.

====================



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class ValidatingTransform {

public static Document transformXMLDocumentWithValidate(Document
in,
Templates template) throws Exception {

if (in == null)
throw (new Exception("Input document is null"));

// What goes here?
SAXResult result = null;

// Get a transformer from the template
Transformer transformer = template.newTransformer();
transformer.transform(new DOMSource(in), result);

// What goes here??
InputSource source = null;

// Parse the output into a dom with validation
// I am assuming the XSLT output will contain a valid
schemaLocation
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
dbFactory.setValidating(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document docResult = dBuilder.parse(source);

return docResult;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top