11.9. The extension Element

The extension element applies to complex types that contain simple content as well as complex types that contain complex content. The extension element provides functionality for adding attribute types to simple content. For complex content, the extension element provides functionality for adding either attributes or nested element types. Finally, with respect to simple content, the base type can be either a simple type or a complex type that contains simple content.

Tip

The use of the final attribute on the base complex type may prohibit the extension of that complex type. In addition, the use of a fixed attribute on a constraining facet of the base complex type may prohibit further modification of that constraining facet.


In the following example, an assembly represents an orderable item made of many smaller parts. Listing 11.14 demonstrates deriving a complex type that specifies this assembly— assemblyCatalogEntryType—from the base baseAssemblyCatalogEntryType by adding a list of part numbers. The simple type partNumberListType represents the list of part numbers.

Listing 11.14. Extending a Complex Type That Contains Complex Content (catalog.xsd)
<xsd:complexType name="assemblyCatalogEntryType" 
                 block="#all" 
                 final="#all" 
                 id="assemblyCatalogEntryType.catalog.cType"> 
    <xsd:annotation>  
        <xsd:documentation xml:lang="en"> 
            The actual definition of an assembly, 
            including the contained parts. 
        </xsd:documentation> 
    </xsd:annotation> 
    <xsd:complexContent> 
        <xsd:extension base="baseAssemblyCatalogEntryType" 
                       id="acet.ext"> 
            <xsd:sequence> 
                <xsd:element name="partList" 
                             type="partNumberListType"/> 
                <xsd:element name="status" 
                             type="assemblyPartStatusType"/> 
            </xsd:sequence> 
        </xsd:extension> 
    </xsd:complexContent> 
</xsd:complexType> 
<xsd:complexType name="baseAssemblyCatalogEntryType" 
                 abstract="true" 
                 block="#all" 
                 id="baseAssemblyCatalogEntryType.catalog.cType"> 
    <xsd:annotation> 
        <xsd:documentation xml:lang="en"> 
            An assembled item is similar to the 
            other catalog entries.  The part number 
            is restricted to an assembly number. 
            In addition, there may be no options. 
            Finally, a part list is also needed. 
            Note that the "includedQuantity" has 
            a default of one, but can be overridden 
            in instances. 
        </xsd:documentation> 
    </xsd:annotation> 
    <xsd:complexContent> 
        <xsd:restriction base="baseCatalogEntryType" 
                         id="bacet.rst"> 
            <xsd:sequence> 
                <xsd:element ref="assemblyID"/> 
                <xsd:element name="partName" 
                             type="partNameType"/> 
                <xsd:element name="partNumber" 
                             type="assemblyPartNumberType"/> 
                <xsd:element name="partOption" 
                             type="partOptionType" 
                             minOccurs="0" 
                             maxOccurs="0"/> 
                <xsd:element name="description"   
                             type="catalogEntryDescriptionType"/> 
                <xsd:group ref="priceGroup"/> 
                <xsd:element name="includedQuantity" 
                             type="xsd:positiveInteger" 
                             default="1"/> 
                <xsd:element name="customerReview" 
                             type="customerReviewType" 
                             minOccurs="0" 
                             maxOccurs="unbounded"/> 
            </xsd:sequence> 
        </xsd:restriction> 
    </xsd:complexContent> 
</xsd:complexType> 

<xsd:simpleType name="partNumberListType" 
                id="catalog.partNumber.list.sType"> 
    <xsd:annotation> 
        <xsd:documentation xml:lang="en"> 
            The "partNumberListType" describes the value 
            for an element that contains a set of part 
            numbers.  Given that a part number might look 
            like any of the following: 
                J1 
                ABC32897 
                ZZ22233344 
           A list of these part numbers might look like: 
                J1 ABC32897 ZZ22233344 
        </xsd:documentation> 
    </xsd:annotation> 
    <xsd:list id="transaction.partNumberList" 
              itemType="partNumberType"> 
    </xsd:list> 
</xsd:simpleType> 

Tip

Use an extension to create an instantiable derivation of an otherwise non-instantiable complex type. Simply do not add any attributes or elements. Listing 11.6 provides an example of using extension in this manner.


11.9.1. Attributes of an extension Element

The only attributes applicable to the extension element are the ubiquitous base attribute and the id attribute. Table 11.7 identifies these two attributes.

Table 11.7. Attribute Summary of an extension Element
Attribute Description
base The value of the base attribute identifies the base simple or complex type of the current derived complex type.
id The value of an id attribute uniquely identifies an element within the set of schema documents that comprise an XML schema.

11.9.1.1. The base Attribute of an extension Element

The value of a base attribute indicates the global base type. For an extension of simple content, the base type can be either a simple type or a complex type with simple content. Otherwise, the derived complex type is an extension of complex content, which means that the value of the base attribute must refer to a global complex type.

Attribute Overview

extension: base

Value: A QName.
Default: None.
Constraints: The base type must refer to a global simple or complex type in the XML schema.
Required: No.


11.9.1.2. The id Attribute of an extension Element

The value of an id attribute uniquely identifies an element within the set of schema documents that comprise an XML schema.

Attribute Overview

extension: id

Value: An ID.
Default: None.
Constraints: An id must be unique within an XML schema.
Required: No.


Listing 11.13 portrays an extension element with an associated id attribute.

11.9.2. Content Options for an extension Element

Both the complexContent and the simpleContent elements may contain an extension element. Some of the content options described in this section apply to the complexContent element, some to the simpleContent element, and some to both. The description of each element in Table 11.8 identifies the appropriate applicability.

Note the Applies To column in Table 11.8. The Applies To column indicates whether the element applies to simple content (the cell value is ‘S’), complex content (the cell value is ‘C’), or both (the cell value is ‘S/C’).

Table 11.8. Content Options for an extension Element
Element Description Applies To
all The all element, discussed in Section 11.11, specifies a model group that indicates an unordered set of element types; an XML instance must reference all the nested element types in any order. C
annotation The annotation element, discussed in Section 7.5, provides a way to document schema elements. S/C
anyAttribute The anyAttribute element, discussed in Section 9.7, specifies a set of namespaces, each of which may provide global attribute types; the XML instance may reference any of these attribute types. S/C
attribute The attribute element, discussed in Section 9.5, specifies a local attribute type or refers to a global attribute type; the XML instance must reference this attribute, unless the attribute is optional or prohibited. S/C
attributeGroup The attributeGroup element, discussed in Section 9.6, specifies a reference to a named attribute-use group. An XML instance must reference each attribute type according to the appropriate nested model group. S/C
choice The choice element, discussed in Section 11.12, specifies a model group that indicates a selection of one element type from a set of element types; an XML instance must reference only one of the element types. C
group The group element, discussed in Section 11.14, references to a named model group. The named model group specifies another model group (that is, all, choice, or sequence). C
sequence The sequence element, covered in Section 11.13, specifies a model group that indicates an ordered set of element types; an XML instance must reference all the nested element types in the schema order. C

The content pattern for an extension element nested within a simpleContent element is:

annotation? (attribute | attributeGroup)* anyAttribute? 

The content pattern for the extension element nested within a complexContent element is:

annotation? 
(group | all | choice | sequence)? 
(attribute | attributeGroup)* anyAttribute? 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.75.227