0

I have a Java program in which I have to validate an XML message against an XSD schema. So far so good. But now I got a new requirement where part of the validation is different based on the content of the XML message.

So based on the value of one field (1) some fields are nillable or not and some enumerations are different. However I can't parse the document before validation... so how should I cope with this?

1) Have 2 different XSD files, handle the XML as text (=ugly) and extract the one field to decide what XSD should be used?

2) Have 3 different XSD files. The 2 different options and one union so we can validate in a way that matches the 2 options? Than we can properly parse the XML, extract the field and decide which more specific XSD to use for extra validation?

3) Other options?

2
  • What do you mean you 'can't parse document before validation'? Commented Mar 30, 2012 at 14:56
  • Well if the document is not validated it could be very well invalid XML so parsing would generate an exception. Commented Apr 2, 2012 at 9:50

2 Answers 2

1

Don't parse it with JAXB then but rather with SAX/StAX type parser (or DOM if you really want to, though SAX would be faster if you just need handful of elements) to locate and analyze element(s) you need in order to determine proper schema which would need to be used. Once you collected enough information, abort parsing and parse again with proper schema validation.

Sign up to request clarification or add additional context in comments.

1 Comment

Maxim, thank you. I wasn't aware I could parse it using SAX without validating first. This solves my issue.
0

Are you writing your own java validation? I would recommend using an open source or paid validation: XML Schema (XSD) validation tool? Tool.

Also from what you are describing it sounds like your schema is no longer backward compatible, since you are having to use two different schemas based on certain elements, i would recommend replying back to the requirement and see if the schema can be changed and updated to maintain backward compatibility. this would then allow the continued use of 1 Schema for your xml files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.