/Jo Vandermeeren/:

I want to parse and validate the entire message in 1 run, and need to use 2 XSD schemas. The XSD's used for validation are packaged inside our application and need to be resolved locally.. I can attach an EntityResolver on the parser, but only one call is issued to resolve the first encountered schemaLocation or noNamespaceSchemaLocation.
It doesn't prompt me for the second one. Any ideas?

Don't know if it would help but the example I'm attaching works for me even without using custom EntityResolver.

The "root" schema contains:

  <xs:any namespace="##other" processContents="lax" />

in the model of the "root" element type and I've intentionally included an invalid "invalid" element in content of the "child" element in the instance document to detect whether the "child" schema comes into play. If the "child" schema is not found no validation will happen for the "child" element and no error will be detected.

--
Stanimir
//package net.example;

import java.io.IOException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class ValidationTest {

    public static void main(String[] args) {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(true);
        
        XMLReader xmlReader;
        try {
            xmlReader = spf.newSAXParser()
                        .getXMLReader();
            xmlReader.setProperty("http://java.sun.com/xml/jaxp/properties/";
                    + "schemaLanguage", "http://www.w3.org/2001/XMLSchema";);
            xmlReader.setProperty("http://java.sun.com/xml/jaxp/properties/";
                    + "schemaSource", new Object[] { "root.xsd", "child.xsd" });
        } catch (SAXException ex) {
            ex.printStackTrace();
            return;
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
            return;
        }
        
        DefaultHandler handler = new DefaultHandler() {
            public void warning(SAXParseException ex) {
                System.err.println(ex);
            }
            public void error(SAXParseException ex) {
                System.err.println(ex);
            }
        };
        xmlReader.setContentHandler(handler);
        xmlReader.setErrorHandler(handler);
        
        URL sourceURL = ValidationTest.class.getResource("test.xml");
        InputSource input = new InputSource(sourceURL.toExternalForm());
        try {
            xmlReader.parse(input);
        } catch (IOException ex) {
            ex.printStackTrace();
            return;
        } catch (SAXException ex) {
            ex.printStackTrace();
            return;
        }
        System.out.println("Done.");
    }

}

Attachment: root.xsd
Description: application/xml

Attachment: child.xsd
Description: application/xml

<?xml version="1.0" encoding="US-ASCII" ?>
<root xmlns="http://www.example.com/root";>
  <inrootnamespace/>
  <child xmlns="http://www.example.com/child";>
    <inchildnamespace/>
    <invalid/>
  </child>
</root>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to