Parsing XML against multiple complex XSD

Discussion in 'Java' started by duncan.idaho2008@googlemail.com, Oct 7, 2008.

  1. Guest

    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
     
    , Oct 7, 2008
    #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. simon ames
    Replies:
    1
    Views:
    506
    Joerg Jooss
    Apr 11, 2004
  2. Replies:
    0
    Views:
    392
  3. Markus
    Replies:
    1
    Views:
    1,109
    Markus
    Nov 22, 2005
  4. Rushi
    Replies:
    1
    Views:
    555
  5. Replies:
    0
    Views:
    562
Loading...

Share This Page