2
votes

I got this from the JAXB doc for @XmlElement

If required() is true, then Javabean property is mapped to an XML schema element declaration with minOccurs="1". maxOccurs is "1" for a single valued property and "unbounded" for a multivalued property.

However when I say

@XmlElement(name = "Name", required = true, nillable = false)

I get the following in the .xsd

<xs:element name="Name" type="xs:string"/>

I wonder how I can make minOccurs to equal to 1. It seems like required = true causes minOccurs to disappear

Edit I realize that the default value is 1. Is there anyway to for minOccurs to show up as 1 in the .xsd

Edit2 I am using JAXBContext.generateSchema to generate the schema (FYI)

3

3 Answers

2
votes

The JAXB spec defines the behaviour:

If required() is true, then Javabean property is mapped to an XML schema element declaration with minOccurs="1". maxOccurs is "1" for a single valued property and "unbounded" for a multivalued property.

The indivual implementations (Metro, EclipseLink MOXy, Apache JaxMe) are free to generate the XML schema as they see fit according to this rule. Metro and MOXy (I'm the tech lead) choose to use the absence of the minOccurs attribute to indicate minOccurs="1".

1
votes

I'm no JAXB expert, but I believe that "minOccurs" is, by default, set to 1 with an xs:element tag. In other words, if the "minOccurs" attribute is missing from the tag, its default is 1.

Edit: Sorry, didn't see your edit until it was too late!

0
votes

The default for xs:element is minOccurs=1. So its possible to omit the attribute and still have the same meaning, which might be what jaxb is doing.

EDIT: Saw your edit too late. I've never seen a way to do this, but that doesn't mean its not possible.