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;
}
}
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;
}
}