|
Michael, Thanks for the reply. I've included my complete test program below. The test only works with the Xerces XMLSchemaFactory if I clone the schema document element each time before it is wrapped by a DOMSource. Is this the expected behavior? I find it interesting that the JDK 1.5 SchemaFactory does not require the schema document element to be cloned? Can you explain the discrepancy? - Ron import java.io.File; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.w3c.dom.Document; import org.w3c.dom.Node; public class TestDOMValidation { { System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl"); } public static void main(String[] args) { if (args.length < 2) { System.err.println ("Usage: java TestDOMValidation " + "[xml filename] [schema filename1] [schema filename2] ..."); System.exit (1); } try { // Get Document Builder Factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Leave off validation, and turn off namespaces factory.setValidating(false); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File(args[0])); Document[] schemaDocs = new Document[args.length - 1]; Source[] constraints = new Source[args.length - 1]; for (int j = 1; j < args.length; j++) { String schemaFilename = args[j]; schemaDocs[j - 1] = builder.parse(new File(schemaFilename)); } SchemaFactory constraintFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); for (int i = 0; i < 2; i++) { for (int j = 1; j < args.length; j++) { Node docRoot = schemaDocs[j - 1].getDocumentElement(); // The docRoot node must be cloned in order for this test work using the Xerces XMLSchemaFactory // The clone is not required using the JAXP 1.3 SchemaFactoryImpl docRoot = docRoot.cloneNode(true); constraints[j - 1] = new DOMSource(docRoot); } // Handle validation Schema schema = constraintFactory.newSchema(constraints); Validator validator = schema.newValidator(); // Validate the DOM tree try { validator.validate(new DOMSource(doc)); System.out.println("Document validates fine."); } catch (org.xml.sax.SAXException e) { System.out.println("Validation error: " + e.getMessage()); } } } catch (ParserConfigurationException e) { System.out.println("The underlying parser does not support the requested features."); } catch (FactoryConfigurationError e) { System.out.println("Error occurred obtaining Document Builder Factory."); } catch (Exception e) { e.printStackTrace(); } } } Michael Glavassevich wrote: You're calling newSchema() twice with the same list of Source objects. In general Sources are not reusable. For instance, if they contain InputStreams or Readers they will have been consumed by the first call to newSchema().Michael Glavassevich XML Parser Development IBM Toronto Lab E-mail: [EMAIL PROTECTED] E-mail: [EMAIL PROTECTED] Ron Gavlin <[EMAIL PROTECTED]> wrote on 09/16/2006 12:13:37 AM: |
- Bogus schema generated after multiple invocations of ... Ron Gavlin
- Re: Bogus schema generated after multiple invoca... Ron Gavlin
- Re: Bogus schema generated after multiple in... Michael Glavassevich
- Re: Bogus schema generated after multipl... Ron Gavlin
- Re: Bogus schema generated after mul... Michael Glavassevich
- Re: Bogus schema generated after multiple invoca... Ron Gavlin
- Re: Bogus schema generated after multiple in... Michael Glavassevich
- Re: Bogus schema generated after multipl... Ron Gavlin
