Groups

Contact Us or call 1-877-932-8228
Groups

Groups

Element and attribute groups can be used to create a set structure for reuse. To illustrate the benefit of groups, let's first look at a simple XML instance and its (rather long) schema that does not use groups.

Code Sample:

ReusingComponents/Demos/WinnieThePooh.xml
<?xml version="1.0"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="Book.xsd">
	<Title>Winnie the Pooh</Title>
	<Author Title="Mr." BirthYear="1882">
		<FirstName>A.</FirstName>
		<MiddleName>A.</MiddleName>
		<LastName>Milne</LastName>
		<Specialty>Childrens</Specialty>
	</Author>
	<Illustrator Title="Mr." BirthYear="1879">
		<FirstName>Ernest</FirstName>
		<MiddleName>H.</MiddleName>
		<LastName>Shepard</LastName>
	</Illustrator>
</Book>

Code Sample:

ReusingComponents/Demos/Book.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="FirstName" type="xs:string"/>
							<xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
							<xs:element name="LastName" type="xs:string"/>
							<xs:element name="Specialty">
								<xs:simpleType>
										<xs:restriction base="xs:string">
											<xs:enumeration value="Mystery"/>
											<xs:enumeration value="Humor"/>
											<xs:enumeration value="Horror"/>
											<xs:enumeration value="Childrens"/>
										</xs:restriction>
								</xs:simpleType>
							</xs:element>
						</xs:sequence>
						<xs:attribute name="Title">
							<xs:simpleType>
								<xs:restriction base="xs:string">
									<xs:enumeration value="Mr."/>
									<xs:enumeration value="Ms."/>
									<xs:enumeration value="Dr."/>
								</xs:restriction>
							</xs:simpleType>
						</xs:attribute>
						<xs:attribute name="BirthYear" type="xs:gYear"/>
					</xs:complexType>
				</xs:element>
				<xs:element name="Illustrator" minOccurs="0">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="FirstName" type="xs:string"/>
							<xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
							<xs:element name="LastName" type="xs:string"/>
						</xs:sequence>
						<xs:attribute name="Title">
							<xs:simpleType>
								<xs:restriction base="xs:string">
									<xs:enumeration value="Mr."/>
									<xs:enumeration value="Ms."/>
									<xs:enumeration value="Dr."/>
								</xs:restriction>
							</xs:simpleType>
						</xs:attribute>
						<xs:attribute name="BirthYear" type="xs:gYear"/>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

The Author element and the Illustrator element have some elements and attributes in common. Let's see how we can make this code more modular.

Element Groups

First, we'll look at how we can group the FirstName, MiddleName, and LastName elements with xs:group to avoid rewriting the elements.

Code Sample:

ReusingComponents/Demos/Book2.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:group name="GroupName">
		<xs:sequence>
			<xs:element name="FirstName" type="xs:string"/>
			<xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
			<xs:element name="LastName" type="xs:string"/>
		</xs:sequence>
	</xs:group>
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:group ref="GroupName"/>
---- C O D E   O M I T T E D ----

					</xs:complexType>
				</xs:element>
				<xs:element name="Illustrator" minOccurs="0">
					<xs:complexType>
						<xs:sequence>
							<xs:group ref="GroupName"/>
						</xs:sequence>
---- C O D E   O M I T T E D ----

					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Attribute Groups

Now let's look at how we can use the xs:attributeGroup element to avoiding rewriting those attributes.

Code Sample:

ReusingComponents/Demos/Book3.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:group name="GroupName">
		<xs:sequence>
			<xs:element name="FirstName" type="xs:string"/>
			<xs:element name="MiddleName" type="xs:string" minOccurs="0"/>
			<xs:element name="LastName" type="xs:string"/>
		</xs:sequence>
	</xs:group>
	<xs:attributeGroup name="AttGroupPerson">
		<xs:attribute name="Title">
			<xs:simpleType>
				<xs:restriction base="xs:string">
					<xs:enumeration value="Mr."/>
					<xs:enumeration value="Ms."/>
					<xs:enumeration value="Dr."/>
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
		<xs:attribute name="BirthYear" type="xs:gYear"/>
	</xs:attributeGroup>
	<xs:element name="Book">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="Title" type="xs:string"/>
				<xs:element name="Author">
					<xs:complexType>
						<xs:sequence>
							<xs:group ref="GroupName"/>
							<xs:element name="Specialty">
								<xs:simpleType>
									<xs:restriction base="xs:string">
										<xs:enumeration value="Mystery"/>
										<xs:enumeration value="Humor"/>
										<xs:enumeration value="Horror"/>
										<xs:enumeration value="Childrens"/>
									</xs:restriction>
								</xs:simpleType>
							</xs:element>
						</xs:sequence>
						<xs:attributeGroup ref="AttGroupPerson"/>
					</xs:complexType>
				</xs:element>
				<xs:element name="Illustrator" minOccurs="0">
					<xs:complexType>
						<xs:sequence>
							<xs:group ref="GroupName"/>
						</xs:sequence>
						<xs:attributeGroup ref="AttGroupPerson"/>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
Next