users@jaxb.java.net

Re: default values for elements?

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Tue, 22 Feb 2005 18:13:56 -0800

Paul A. Thiessen wrote:
> Would something like this be possible? Or maybe there's some other way to
> specify a default value in the schema? I know defaults can be set up for
> attributes, but is there a way to set a default for an actual (simple type)
> element value?

Default values for elements are very different beast than default values
for attributes. In attributes, an attribute can be either present, or
absent (=defaulted).

In elements, an element can be absent, nil-ed (<foo xsi:nil='true'/>),
defaulted (<foo/>), or present (<foo>something</foo>), and then you can
repeat this whole thing as many as you want, by having
maxOccurs='unbounded'.

So it really doesn't fit very well to the goal of generating a simple
Java class. For example, with your following example:

> <!-- Imprint group -->
> <xs:element name="Imprint">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="Imprint_date"/>
> <xs:element ref="Imprint_volume" minOccurs="0"/>
> <xs:element ref="Imprint_issue" minOccurs="0"/>
> <xs:element ref="Imprint_pages" minOccurs="0"/>
> <xs:element ref="Imprint_section" minOccurs="0"/>
> <xs:element ref="Imprint_language" minOccurs="0"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> ..
>
> <xs:element name="Imprint_language" type="xs:string" default="ENG"/>

You already have to distinguish

<Imprint>
   ...
   <!-- no Imprint_language -->
</Imprint>

and

<Imprint>
   ...
   <Imprint_language/> <!-- defaulted -->
</Imprint>

and

<Imprint>
   ...
   <Imprint_language>ENG</Imprint_language>
</Imprint>


In 2.0, the above definition generates something like:

public class Imprint {
   protected String imprintLanguage;

   public String getImprintLanguage() {
     return imprintLanguage;
   }
   ...
}

We'd like to solve this problem by allowing you to modify the generated
code. There, you can compile this schema, and then manually modify it to:

   protected String imprintLanguage = "ENG";




-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com