Creating Attribute Groups

You can also create groups of attributes using the <xsd:attributeGroup> element. For example, say that I wanted to add a number of attributes to the <book> element that describe the book. To do that, I can create an attribute group named bookDescription and then reference that attribute group in the declaration for <book>:

<xsd:element name="book">
    <xsd:complexType>
        <xsd:element name="bookTitle" type="xsd:string"/>
        <xsd:element name="pubDate" type="xsd:date" minOccurs='0'/>
        <xsd:element name="replacementValue" type="xsd:decimal"/>
        <xsd:element name="maxDaysOut">
           <xsd:simpleType base="xsd:integer">
                <xsd:maxExclusive value="14"/>
            </xsd:simpleType>
        </xsd:element>
        <xsd:attributeGroup ref="bookDescription"/>
    </xsd:complexType>
</xsd:element>

To create the attribute group bookDescription, I just use the <xsd:attributeGroup> element, enclosing the <xsd:attribute> elements that I use to declare the attributes in the <xsd:attributeGroup> element:

<xsd:attributeGroup name="bookDescription">
    <xsd:attribute name="bookID" type="CatalogID"/>
    <xsd:attribute name="numberPages" type="xsd:decimal"/>
    <xsd:attribute name="coverType">
        <xsd:simpleType base="xsd:string">
            <xsd:enumeration value="leather"/>
            <xsd:enumeration value="cloth"/>
            <xsd:enumeration value="vinyl"/>
        </xsd:simpleType>
    </xsd:attribute>
</xsd:attributeGroup><xsd:element name="book">
    <xsd:complexType>
        <xsd:element name="bookTitle" type="xsd:string"/>
        <xsd:element name="pubDate" type="xsd:date" minOccurs='0'/>
        <xsd:element name="replacementValue" type="xsd:decimal"/>
        <xsd:element name="maxDaysOut">
           <xsd:simpleType base="xsd:integer">
                <xsd:maxExclusive value="14"/>
            </xsd:simpleType>
        </xsd:element>
        <xsd:attributeGroup ref="bookDescription"/>
    </xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="bookDescription">
    <xsd:attribute name="bookID" type="CatalogID"/>
    <xsd:attribute name="numberPages" type="xsd:decimal"/>
    <xsd:attribute name="coverType">
        <xsd:simpleType base="xsd:string">
            <xsd:enumeration value="leather"/>
            <xsd:enumeration value="cloth"/>
            <xsd:enumeration value="vinyl"/>
        </xsd:simpleType>
    </xsd:attribute>
</xsd:attributeGroup>

Groups Versus Parameter Entities

The process of creating a group of elements or attributes and then referencing that group in another element mimics the use of parameter entities in DTDs. With DTDs, you do the same thing—include a group, elements, or attributes—in a more or less similar way. There are no such things as parameter entities in schemas, but using groups, you can accomplish most of what parameter entities are used for in DTDs.


As you can see, schemas provide some sophisticated mechanisms for building documents up from pieces.

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

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