FUSE Services Framework supports the use of attribute
elements and
attributeGroup
elements within the scope of a complexType
element.
When defining structures for an XML document attribute declarations provide a means of adding information that is specified within
the tag, not the value that the tag contains. For example, when describing the XML element
<value currency="euro">410<\value> in XML Schema the currency
attribute is described using an attribute
element as shown in
Example 13.5.
The attributeGroup
element allows you to define a group of reusable attributes that can be
referenced by all complex types defined by the schema. For example, if you are defining a series of elements that all use the
attributes category
and pubDate
, you could define an attribute group
with these attributes and reference them in all the elements that use them. This is shown in
Example 13.7.
When describing data types for use in developing application logic, attributes whose use
attribute is set to either optional
or required
are treated as elements
of a structure. For each attribute declaration contained within a complex type description, an element is generated in the class for
the attribute, along with the appropriate getter and setter methods.
An XML Schema attribute
element has one required attribute,
name
, that is used to identify the attribute. It also has four optional attributes that are described in
Table 13.2.
Table 13.2. Optional Attributes Used to Define Attributes in XML Schema
Attribute | Description |
---|---|
use | Specifies if the attribute is required. Valid values are required , optional , or prohibited . optional is the default value. |
type | Specifies the type of value the attribute can take. If it is not used the schema type of the attribute must be defined in-line. |
default | Specifies a default value to use for the attribute. It is only used when the attribute element's use attribute is set to optional . |
fixed | Specifies a fixed value to use for the attribute. It is only used when the attribute element's use attribute is set to optional . |
Example 13.5 shows an attribute element defining an attribute, currency, whose value is a string.
Example 13.5. XML Schema Defining and Attribute
<element name="value"> <complexType> <xsd:simpleContent> <xsd:extension base="xsd:integer"> <xsd:attribute name="currency" type="xsd:string" use="required"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element>
If the type
attribute is omitted from the attribute
element, the format of the data must be described in-line. Example 13.6 shows an attribute
element for an attribute, category
, that can take the values autobiography
, non-fiction
, or fiction
.
Example 13.6. Attribute with an In-Line Data Description
<attribute name="category" use="required"> <simpleType> <restriction base="xsd:string"> <enumeration value="autobiography"/> <enumeration value="non-fiction"/> <enumeration value="fiction"/> </restriction> </simpleType> </attribute>
Using an attribute group in a complex type definition is a two step process:
Define the attribute group.
An attribute group is defined using an attributeGroup
element with a number of
attribute
child elements. The attributeGroup
requires a
name
attribute that defines the string used to refer to the attribute group. The
attribute
elements define the members of the attribute group and are specified as shown in
Defining an attribute in XML Schema. Example 13.7
shows the description of the attribute group catalogIndecies
. The attribute group has two members:
category
, which is optional, and pubDate
, which is
required.
Example 13.7. Attribute Group Definition
<attributeGroup name="catalogIndices"> <attribute name="category" type="catagoryType" /> <attribute name="pubDate" type="dateTime" use="required" /> </attributeGroup>
Use the attribute group in the definition of a complex type.
You use attribute groups in complex type definitions by using the attributeGroup
element with the ref
attribute. The value of the ref
attribute is
the name given the attribute group that you want to use as part of the type definition. For example if you want to use the
attribute group catalogIndecies
in the complex type dvdType, you would use
<attributeGroup ref="catalogIndecies" /> as shown in
Example 13.8.
Example 13.8. Complex Type with an Attribute Group
<complexType name="dvdType">
<sequence>
<element name="title" type="xsd:string" />
<element name="director" type="xsd:string" />
<element name="numCopies" type="xsd:int" />
</sequence>
<attributeGroup ref="catalogIndices" />
</complexType>
Attributes are mapped to Java in much the same way that member elements are mapped to Java. Required attributes and
optional attributes are mapped to member variables in the generated Java class. The member variables are decorated with the
@XmlAttribute
annotation. If the attribute is required, the
@XmlAttribute
annotation's required property is set to
true
.
The complex type defined in Example 13.9 is mapped to the Java class shown in Example 13.10.
Example 13.9. techDoc Description
<complexType name="techDoc"> <all> <element name="product" type="xsd:string" /> <element name="version" type="xsd:short" /> </all> <attribute name="usefullness" type="xsd:float" use="optional" default="0.01" /> </complexType>
Example 13.10. techDoc Java Class
@XmlType(name = "techDoc", propOrder = { }) public class TechDoc { @XmlElement(required = true) protected String product; protected short version; @XmlAttribute protected Float usefullness; public String getProduct() { return product; } public void setProduct(String value) { this.product = value; } public short getVersion() { return version; } public void setVersion(short value) { this.version = value; } public float getUsefullness() { if (usefullness == null) { return 0.01F; } else { return usefullness; } } public void setUsefullness(Float value) { this.usefullness = value; } }
As shown in Example 13.10, the default
attribute and the fixed
attribute instruct the code generators to add code to the getter method generated for the attribute. This additional code ensures that the specified value is returned if no value is set.
![]() | Important |
---|---|
The |
Attribute groups are mapped to Java as if the members of the group were explicitly used in the type definition. If the
attribute group has three members, and it is used in a complex type, the generated class for that type will include a member
variable, along with the getter and setter methods, for each member of the attribute group. For example, the complex type defined
in Example 13.8, FUSE Services Framework generates a class containing the member
variables category
and pubDate
to support the members of the attribute
group as shown in Example 13.11.
Example 13.11. dvdType Java Class
@XmlType(name = "dvdType", propOrder = {
"title",
"director",
"numCopies"
})
public class DvdType {
@XmlElement(required = true)
protected String title;
@XmlElement(required = true)
protected String director;
protected int numCopies;
@XmlAttribute
protected CatagoryType category;
@XmlAttribute(required = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar pubDate;
public String getTitle() {
return title;
}
public void setTitle(String value) {
this.title = value;
}
public String getDirector() {
return director;
}
public void setDirector(String value) {
this.director = value;
}
public int getNumCopies() {
return numCopies;
}
public void setNumCopies(int value) {
this.numCopies = value;
}
public CatagoryType getCatagory() {
return catagory;
}
public void setCatagory(CatagoryType value) {
this.catagory = value;
}
public XMLGregorianCalendar getPubDate() {
return pubDate;
}
public void setPubDate(XMLGregorianCalendar value) {
this.pubDate = value;
}
}