Designing Your Own XML Schema: Indicators - Attribute Groups in XML Schema
(Page 6 of 6 )
This concept is very similar to “groups” in XML Schema (as described in the previous section). The only difference is that, instead of defining elements inside the group (as in the previous section), we define frequently repeated attributes within the group, forming an “attribute group.”
Once an “Attribute Group” is defined with its own name, it can be applied at several instances within the same schema (or even external schemas as well). Let us consider the following schema.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
<xs:element name="Employees">
<xs:annotation>
<xs:documentation>Contains All Employee information</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:string" />
<xs:element name="Name" type="xs:string" />
<xs:element name="ResAddress">
<xs:complexType>
<xs:attributeGroup ref="attrAddress" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="PK_Employee_ID">
<xs:selector xpath=".//Employee" />
<xs:field xpath="ID" />
</xs:key>
</xs:element>
<xs:attributeGroup name="attrAddress">
<xs:attribute name="Street" type="xs:string" />
<xs:attribute name="City" type="xs:string" />
<xs:attribute name="Zip" type="xs:string" />
</xs:attributeGroup>
</xs:schema>
The most important fragment to be concentrated on is the following:
<xs:attributeGroup name="attrAddress">
<xs:attribute name="Street" type="xs:string" />
<xs:attribute name="City" type="xs:string" />
<xs:attribute name="Zip" type="xs:string" />
</xs:attributeGroup>
The above defines that there exists three attributes, “Street,” “City” and “Zip” which are together grouped (or encapsulated) as “attrAddress.” The following code snippet makes use of it accordingly when necessary:
<xs:element name="ResAddress">
<xs:complexType>
<xs:attributeGroup ref="attrAddress" />
</xs:complexType>
</xs:element>
The above snippet defines a new element, “ResAddress,” which would have all the attributes defined within “attrAddress.” The following is a sample XML document, which conforms to the above XML Schema:
<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Documents and
Settings\Administrator\Desktop\Employees.xsd">
<Employee>
<ID/>
<Name/>
<ResAddress Street="13-20-26" City="Bhimavaram"
Zip="534201"/>
</Employee>
</Employees>
From all of the articles in this series, any reader should be confident enough to design their own pretty XML schemas very easily without many problems. Though I didn’t touch the most advanced topics (yet), you can expect them soon. Keep referring to this website frequently or sign up for a newsletter to find out when new articles get published.
Any comments, suggestions, feedback, bugs, errors are highly appreciated at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |