33.2. Attributes
Overview
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 33.5, “XML Schema Defining and Attribute”.
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 33.7, “Attribute Group Definition”.
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.
Defining an attribute in 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 33.2, “Optional Attributes Used to Define Attributes in XML Schema”.
Table 33.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 33.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>type attribute is omitted from the attribute element, the format of the data must be described in-line. Example 33.6, “Attribute with an In-Line Data Description” shows an attribute element for an attribute, category, that can take the values autobiography, non-fiction, or fiction.
Example 33.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 XML Schema
- Define the attribute group.An attribute group is defined using an
attributeGroupelement with a number ofattributechild elements. TheattributeGrouprequires anameattribute that defines the string used to refer to the attribute group. Theattributeelements define the members of the attribute group and are specified as shown in the section called “Defining an attribute in XML Schema”. Example 33.7, “Attribute Group Definition” shows the description of the attribute groupcatalogIndecies. The attribute group has two members:category, which is optional, andpubDate, which is required.Example 33.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
attributeGroupelement with therefattribute. The value of therefattribute 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 groupcatalogIndeciesin the complex type dvdType, you would use <attributeGroup ref="catalogIndecies" /> as shown in Example 33.8, “Complex Type with an Attribute Group”.Example 33.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>
Mapping attributes to Java
@XmlAttribute annotation. If the attribute is required, the @XmlAttribute annotation's required property is set to true.
Example 33.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 33.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;
}
}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.
fixed attribute is treated the same as the default attribute. If you want the fixed attribute to be treated as a Java constant you can use the customization described in Section 36.5, “Customizing Fixed Value Attribute Mapping”.
Mapping attribute groups to Java
category and pubDate to support the members of the attribute group as shown in Example 33.11, “dvdType Java Class”.
Example 33.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;
}
}
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.