How to validate XSLT output in java?

Discussion in 'Java' started by Damon Rand, Jul 29, 2004.

  1. Damon Rand

    Damon Rand Guest

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

    }
    Damon Rand, Jul 29, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duane Morin
    Replies:
    1
    Views:
    848
    Patrick TJ McPhee
    Dec 11, 2003
  2. ted
    Replies:
    1
    Views:
    619
    Laurens
    Jan 26, 2004
  3. Juho Jussila
    Replies:
    1
    Views:
    504
    Stylus Studio
    Dec 2, 2005
  4. C.W.Holeman II
    Replies:
    7
    Views:
    683
    C.W.Holeman II
    May 30, 2007
  5. phal
    Replies:
    12
    Views:
    222
Loading...

Share This Page