Skip to main content

@XmlElementRef produces invalid schema

4 replies [Last post]
tigera
Offline
Joined: 2007-07-15
Points: 0

Greetings,

I have written two classes, which are annotated separately, and I am trying to use schemagen to produce the XML Schema for them:

<br />
@XmlRootElement<br />
@XmlType(name="ResourceType",propOrder={})<br />
public class ResourceType {</p>
<p>    private Vector basicElems;</p>
<p>    public ResourceType()<br />
    /**<br />
     * Simple constructor.  Initializes a non-classified resource type.<br />
     */<br />
    {<br />
        basicElems = new Vector();<br />
    }</p>
<p>@XmlElementRef<br />
    public Vector getBasicElems()<br />
    /**<br />
     * Gets all individual components of this resource type.<br />
     */<br />
    {<br />
        return new Vector(basicElems);<br />
    }</p>
<p>    public void setBasicElems(Vector val) {<br />
        this.basicElems = val;<br />
    }<br />
}</p>
<p>@XmlRootElement<br />
@XmlType(name="Property",propOrder={})<br />
public class Property<br />
{<br />
    private String name;</p>
<p>    private HashSet excludes;</p>
<p>    public Property() {<br />
        name = new String();<br />
        excludes = new HashSet();<br />
    }</p>
<p>    public Property(String n)<br />
    {<br />
        name = n;<br />
        excludes = new HashSet();<br />
    }</p>
<p>    @XmlAttribute<br />
    public String getName() {<br />
        return name;<br />
    }</p>
<p>    public void setName(String val) {<br />
        this.name = val;<br />
    }</p>
<p>    @XmlElementWrapper<br />
    public HashSet getExcludes() {<br />
        return excludes;<br />
    }</p>
<p>    public void setExcludes(HashSet val) {<br />
        this.excludes = val;<br />
    }<br />
}<br />

The output of schemagen is this:

<br />
<?xml version="1.0" encoding="UTF-8" standalone="yes"?></p>
<p>

When validating, I receive this error:
org.xml.sax.SAXParseException: cos-all-limited.2: The {max occurs} of an element in an 'all' model group must be 0 or 1. The value '-1' for element 'property' is invalid.

I tried annotating the basicElems field instead, but then schemagen gave me this error:
error: Class has two properties of the same name "basicElems"

I am new to JAXB, and I'm sure that I'm doing something wrong, but I can't figure out how to fix this. May I please have some help?

Thanks...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tigera
Offline
Joined: 2007-07-15
Points: 0

OK, I've got it. I've managed to clean up the schema considerably with your help by removing the propOrder attribute and changing the @XmlElementWrappers into @XmlElements. Now, I've added another class, but neither of those tricks seem to work:

[code]
@XmlRootElement
@XmlType()
public class Resource implements Serializable {

private static final long serialVersionUID = 1L;

private HashMap values;

@XmlElementWrapper
public java.util.HashMap getValues() {
return values;
}

public void setValues(HashMap val) {
this.values = val;
}

}
[/code]

Trying to use @XmlElement, either directly on the field or on the accessor, generates an abstract type in the schema. The current schema that gets generated (with the element wrapper, and a lot of extra complexTypes) is this:

[code]




























[/code]

What can I do to clean this up?

Thanks again....

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

I'm sorry, what is it that you wanted to do?

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

We could have detected this error upfront, but this is often useful and have no problems unless you generate a schema, so I guess that's why it's left unreported.

You can work around the problem by not having propOrder={}, which makes this a sequence, not all.

tigera
Offline
Joined: 2007-07-15
Points: 0

Sweet! I'll give that a try. I hadn't heard from anyone in so long that I'd almost given up hope. Thanks for the help!