33.2. Attributes

download PDF


Apache CXF 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 33.5, “XML Schema Defining and Attribute”.
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 33.7, “Attribute Group Definition”.
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.

Defining an attribute in XML Schema

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 33.2, “Optional Attributes Used to Define Attributes in XML Schema”.
Table 33.2. Optional Attributes Used to Define Attributes in XML Schema
useSpecifies if the attribute is required. Valid values are required, optional, or prohibited. optional is the default value.
typeSpecifies the type of value the attribute can take. If it is not used the schema type of the attribute must be defined in-line.
defaultSpecifies a default value to use for the attribute. It is only used when the attribute element's use attribute is set to optional.
fixedSpecifies 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” shows an attribute element defining an attribute, currency, whose value is a string.

Example 33.5. XML Schema Defining and Attribute

<element name="value">
      <xsd:extension base="xsd:integer">
        <xsd:attribute name="currency" type="xsd:string"
If the 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">
    <restriction base="xsd:string">
      <enumeration value="autobiography"/>
      <enumeration value="non-fiction"/>
      <enumeration value="fiction"/>

Using an attribute group in XML Schema

Using an attribute group in a complex type definition is a two step process:
  1. 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 the section called “Defining an attribute in XML Schema”. Example 33.7, “Attribute Group Definition” shows the description of the attribute group catalogIndecies. The attribute group has two members: category, which is optional, and pubDate, which is required.

    Example 33.7. Attribute Group Definition

    <attributeGroup name="catalogIndices">
      <attribute name="category" type="catagoryType" />
      <attribute name="pubDate" type="dateTime"
                 use="required" />
  2. 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 33.8, “Complex Type with an Attribute Group”.

    Example 33.8. Complex Type with an Attribute Group

    <complexType name="dvdType">
        <element name="title" type="xsd:string" />
        <element name="director" type="xsd:string" />
        <element name="numCopies" type="xsd:int" />
      <attributeGroup ref="catalogIndices" />

Mapping attributes to Java

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 33.9, “techDoc Description ” is mapped to the Java class shown in Example 33.10, “techDoc Java Class”.

Example 33.9. techDoc Description

<complexType name="techDoc">
    <element name="product" type="xsd:string" />
    <element name="version" type="xsd:short" />
  <attribute name="usefullness" type="xsd:float"
             use="optional" default="0.01" />

Example 33.10. techDoc Java Class

@XmlType(name = "techDoc", propOrder = {

public class TechDoc {

    @XmlElement(required = true)
    protected String product;
    protected short version;
    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 33.10, “techDoc Java Class”, 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.
The 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

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 33.8, “Complex Type with an Attribute Group”, Apache CXF generates a class containing the member variables 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 = {
public class DvdType {

    @XmlElement(required = true)
    protected String title;
    @XmlElement(required = true)
    protected String director;
    protected int numCopies;
    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;

Red Hat logoGithubRedditYoutubeTwitter


Try, buy, & sell


About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.