Skip to main content

What annotation to use to restrict maxOccurs/minOccurs ?

8 replies [Last post]
amarkel
Offline
Joined: 2004-05-13
Points: 0

I am generating xsd from Java.

I am trying to create an xsd that would create a 'choice'
between two different types, while restricting the number
of instances.

The following is what I did:

@XmlElements( {
@XmlElement( type = foo.Record.class,
name = "record" ),
@XmlElement( type = foo.Table.class,
name = "table" )
}
)

// foo.Record and foo.Table extend foo.Content
//
protected List content;

A snippet of the xsd that results is:

----

I tried replacing with:

@XmlElements( {
@XmlElement( type = foo.Record.class,
name = "record" ),
@XmlElement( type = foo.Table.class,
name = "table" )
}
)

// foo.Record and foo.Table extend foo.Content
//
protected foo.Content content;

But it did not make it a 'choice'.

I would like the result to be (maxOccurs=1) :

How can one force to make a 'choice' without making
it an unbounded-length list ?

Arieh

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
amarkel
Offline
Joined: 2004-05-13
Points: 0

I am still having trouble with this ...

I am trying to create an element that forces to
create a single instance:

When doing it in Java:

@XmlAccessorType(AccessType.PROPERTY)
@XmlType( name = "tag" )
public class Tag
{
private Record theRec;

public Tag() {}
public Tag( Record rec ) { setRecord( rec ); }

@XmlElement( name = "record", nillable = false )
public Record getRecord() { return record;}
public void setRecord( Record theRec ) {
record = theRec;
}
}

public Record {
public Record(){}
}

I don't know how to force the 'minOccurs="1"'

Am I missing something ?

Arieh

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Generally speaking, the degree in which you can control the generated schema is somewhat limited.

I believe there was a discussion about adding "boolean @XmlElement.required()" to address this, but it doesn't look like we have it right now.

I'll let the spec team know to see if they want to address this.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

The 2nd schema fragment you got is a choice with maxOccurs=1.

amarkel
Offline
Joined: 2004-05-13
Points: 0

I know that I can make a choice with maxOccurs="1".

However, I am starting from java and want to generate xsd.

What in Java (annotation ?) will effect a maxOccurs="1" ?

Arieh

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Sorry, I think your 2nd example:

----------------------
@XmlElements( {
@XmlElement( type = foo.Record.class,
name = "record" ),
@XmlElement( type = foo.Table.class,
name = "table" )
}
)
protected foo.Content content;
----------------------

should produce what you want.

amarkel
Offline
Joined: 2004-05-13
Points: 0

I thought so too.

However it did not create that ...

Arieh

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Ah, you are right! It's a bug in the RI.
Working on a fix right now.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

fixed.