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: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>  

Using Groups

It's worth noticing that 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. You do the same thing in a fairly similar way: include a group or elements or attributes. 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 from pieces.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net