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:sequence> <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> <xsd:restriction base="xsd:integer"> <xsd:maxExclusive value="14"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> <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:element name="book"> <xsd:complexType> <xsd:sequence> <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> <xsd:restriction base="xsd:integer"> <xsd:maxExclusive value="14"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> <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> <xsd:restriction base="xsd:string"> <xsd:enumeration value="leather"/> <xsd:enumeration value="cloth"/> <xsd:enumeration value="vinyl"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:attributeGroup>
As you can see, schemas provide some sophisticated mechanisms for building documents from pieces. |