0

I am validating an xml instance against multiple XSD schemas. If validation fails, I want to determine which schema the xml instance failed against. The SAXParseException does not contain enough information to determine this.

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Source[] sources = getXsdSources();
Schema schema = factory.newSchema(sources);
Validator validator = schema.newValidator();

try {
  validator.validate(input);
} catch (SAXParseException e) {
  // Error handling.
}
Joe W
  • 1,789
  • 3
  • 28
  • 42

3 Answers3

0

@Joe, The following link might help you. You can find same question asked in Stackoverflow earlier. Validate an XML File Against Multiple Schema Definitions

Community
  • 1
  • 1
Anvesh Vejandla
  • 221
  • 1
  • 2
  • 11
  • I've looked at that post. The issue the OP was having was that some of the schemas wouldn't resolve correctly. I am not having any issues loading the schemas, what I'm trying to do, is figure out which schema my instance fails against. – Joe W Oct 26 '12 at 16:24
  • Unless of course, I missed something in the posts. Could you please indicate in your answer what the actual solution is instead of just referencing another post without additionally posting the relevant solution. (Think what happens if that link becomes dead.) – Joe W Oct 26 '12 at 16:26
  • @Joe: I am newbibe in java. I done this way earlier. I have loaded StreamInstanceValidator jar file to my classpath and used the below command for validating in command prompt. java org.apache.xmlbeans.impl.tool.StreamInstanceValidator -dl %2.xml %5.xsd > %2.txt – Anvesh Vejandla Oct 26 '12 at 16:33
  • I have absolutely no idea what your comment means. Additionally, as I mentioned previously in my other comment, if you found a relevant solution in the other post, please add that to your answer instead of just posting a link. – Joe W Oct 26 '12 at 16:38
0

you will need to validate each schema individually instead of loading them all into 'schema'. then its a simple matter of iterating over the list of 'sources' setting 'validator' to each one, so you can keep track of which was loaded at the time of failure.

this is not tested, but should be about right:

SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    Source[] sources = getXsdSources();
    foreach( Source s : sources){
    Schema schema = factory.newSchema(s);
    Validator validator = schema.newValidator();
    try {
      validator.validate(input);
    } catch (SAXParseException e) {
      // Error handling.
      System.out.println("failed on " + s.getSystemID)
    }
}
Frank Thomas
  • 2,434
  • 1
  • 17
  • 28
  • I've tried that approach and the challenge I've had is that if I validate against a sub schema that isn't the root type, then I get `SAXParseException: cvc-elt.1: Cannot find the declaration of element 's:MyElement'`. So if schemaA imports schemaB and uses schemaB on internal elements, if I validate exclusively against schemaB, validation will fail with the aforementioned exception. – Joe W Oct 26 '12 at 16:30
0

Xerces J provides access to all (or possibly only almost all?) of the post-schema validation infoset (PSVI), which includes information about the type against which an element or attribute was validated and the nature of any invalidity. See http://xerces.apache.org/xerces2-j/faq-xs.html#faq-8 for more info.

If the validator you're using is Xerces J, the information you need is available (perhaps in the exception wrapped by the SAXParseException you are handling?); if you're using another validator, you can either explore its API a bit more or you can use Xerces J.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • I ended up refactoring my validation process to only pass in the schema for the root namespace. Although I still won't be able to tell if it was the main schema or an imported schema, I figured pointing them to the root schema would be better than nothing. I looked in Xerces and it might work, but have not tried implementing it. – Joe W Nov 09 '12 at 17:49