tags:

views:

45

answers:

2

Hi all,

I use JAXB 2.1 to generate Java classes from several XSD files that I cannot modify (coming from a WSDL file actually), and I have a problem related to complex type restriction. On of the restrictions modifies the occurence configuration from minOccurs="0" maxOccurs="unbounded" to minOccurs="0" maxOccurs="0". Thus this field is not needed anymore in the restricted type. But actually JAXB generates the restricted class with a [0..1] cardinality instead of 0.

By the way the generation is tuned with <xjc:treatRestrictionLikeNewType /> so that a XSD restriction is not mapped to a Java class inheritance.

Here is an example:

Here is the way a field is defined in a complex type A:

<element name="qualifier" type="CR" maxOccurs="unbounded" minOccurs="0"/>

Here is the way the same field is restricted in another complex type B that restricts A:

<element name="qualifier" type="CR" minOccurs="0" maxOccurs="0"/>

In the A generated class I have:

@XmlElement(name = "qualifier")
protected List<CR> qualifiers;

And in the B generated class I have:

protected CR qualifiers;

With my poor understanding of JAXB the absence of the XmlElement annotation tells JAXB not to marshall/unmarshall this field. Am I wrong? If I am right is there a way to tell JAXB not to generate the qualifiers field at all? This would be in my opinion a much better generation as it respects the constraints.

Any idea, thougths on the topic?

Thanks!!

A: 

If the type is not needed then alone minOccurs="0" should be enough, no ? Looks like you are trying to define an element that may not be included in the document - that does not make sense, although it's valid.

binary_runner
Yes you're right but I do not have the hand on the XSD files.I have to deal with it ;)
reef
A: 

Ok I figured it out.

This is basically an interaction issue between the <xjc:treatRestrictionLikeNewType /> customization and the <xjc:simple /> one. If I remove the simple one the fields are not generated anymore!

That's what happens when you walk on the experimental path of JAXB ;)

reef