views:

21

answers:

1

A very generalized and simplified scenario description:

  • Application builds JAXB instances through UI,
  • The JAXB instances are built in different steps are composed in the end,
  • As this is done, I must perform validation of the JAXB instances created in each step against the XSD to verify that the UI tier does not build nonsense.

Now the schema defines complex types A, B and C. The schema specifies that XML document must have top level element A and this can contain multiple B and this can optionally include C. I create JAXB instance for B and absolutely want to validate this against the complex type definition in the XSD before nesting it under A. However validation of this will fail if I validate against the entire XSD.

Questions: How to validate JAXB instance against only a part of the XSD from which its class has been generated from?

How about using schemagen to generate schema from the JAXB class which instance I want to validate and then validate against that? Do you think that can work? Any other ideas?

I have no previous experience with schemagen and will start prototyping of this solution soon.

Note: in reality, the schemas are not as simple as in the above example and the solution of creating some always-valid mock of A is not feasible option. Not to mention that this kind of validation will be on hundred places to say the least.

A: 

Well it turns out that using xsi:type let's one accomplish this.

http://www.w3.org/TR/xmlschema-1/#xsi_type

finrod