Parsing XML against multiple complex XSD



Good morning

I have an XML file that I need to validate in code
The schema definition is spread accross multiple XSD files
there is 'top level' XSD that 'includes' another XSD that 'includes'
multiple other XSDs

All these are in the classpath and can be loaded like so

Source[] sources = new Source[15];

sources[0] = new
StreamSource(getClass().getClassLoader().getResourceAsStream("schema/
YearTypes-v1-0.xsd"));
sources[1] = new
StreamSource(getClass().getClassLoader().getResourceAsStream("schema/
WelshDetails-v1-0.xsd"));
sources[2] = new
StreamSource(getClass().getClassLoader().getResourceAsStream("schema/
UPNtype-v2-0.xsd"));
sources[3] = new
StreamSource(getClass().getClassLoader().getResourceAsStream("schema/
SuppInfo-v1-0.xsd"));
etc etc

I then create a SchemaFactory

SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Schema schema = null;
try {
schema = factory.newSchema(sources);
}
catch (SAXException e) {
e.printStackTrace();
//throw new SchemaValidationException(e.getMessage());
}

This seems to work as schema is not null

However when I try to validate an XML file like so

Validator validator = schema.newValidator();
File xml = new File(.../examplectf.xml";);
StreamSource ss = new StreamSource(xml

try {
validator.validate(ss);
....


Validation fails with the following validationException

cvc-elt.1: Cannot find the declaration of element 'CTfile'.

However CTFile is declared in the top level XSD

I think it may have something to do with the 'includes' however my
reading of the javadoc seems to imply that loading an array with
sources and using this to create the schema factory should combine all
the files.

Does anyone have experience og validating against multiple XSDs in
this way

Thanks
Idaho
.



Relevant Pages

  • Re: xsd.exe cannot generate schema
    ... xsd.exe cannot generate a schema from such an xml file. ... For example, write the XSD like ...
    (microsoft.public.dotnet.xml)
  • Re: Validating XML against schema (XSD) possible with TCL?
    ... I only need to read an XML file and validate it (+ use the data of ... If the schema was hand generated and not spit out from a Java or .NET ... it could be easy to reconstruct the schema in tWSDL. ...
    (comp.lang.tcl)
  • Re: xsd.exe cannot generate schema
    ... thought it should be dealt with by default by any schema inferencing tool ... The reason I ask is that BizTalk makes a lot of use of xsd schemas. ... xsd.exe cannot generate a schema from such an xml file. ... >> The xml file has a mix of defined namespaces and also an empty namespace. ...
    (microsoft.public.dotnet.xml)
  • Re: XML Schema validation in tcl
    ... Are you trying to test your Tcl script, or the XSD schema? ... We need to validate an XML file against a set of xsd files in Tcl. ...
    (comp.lang.tcl)
  • Re: parsing XML file
    ... want to validate the XML file by a XSD ... Schema file I have. ... The Java code to validate with an XSD schema is trivial. ...
    (comp.lang.java.programmer)