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?

hcpl
  • 17,382
  • 7
  • 72
  • 73

2 Answers2

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.

maximdim
  • 8,041
  • 3
  • 33
  • 48
  • Maxim, thank you. I wasn't aware I could parse it using SAX without validating first. This solves my issue. – hcpl May 31 '12 at 08:43
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.

Community
  • 1
  • 1
Jtello
  • 752
  • 1
  • 5
  • 17