XML Schema Definition Part 5


XML with Header and Detail Sections

Going yet another step forward into the natural complexity of the real world, we will look at an order of books in XML that has a header section at the top of the XML file.

The following table documents our requirements for our project. The section column shows us the levels. The root is Order. It describes the entire XML file. Under that there are two sections: Header and Detail. I recommend that you start with the data itself. Document what data elements you need and how you will organize them. Consider naming conventions, data types, data restrictions such as string length and multiplicity.

Section Tag Base Type Multiplicity Other
1 Order [1..1]
2 Header [1..1]
2.1 HeaderID string [1..1] maxLength=15
2.2 HeaderName string [1..1] maxLength=15
3 Detail [1..1]
3.1 Books [1..1]
3.1.1 Book [1..n] maxOccurs=unbounded
3.1.1.1 Title string [1..1] maxLength=20
3.1.1.2 Author string [1..1] maxLength=15
Notes: Multiplicity refers the the lowest and highest occurrences of an element, therefore, [1..1] means that it is required and occurs exactly one time. We do not actually need the Books tag, but it is included for illustration in the XSD document. Many XML documents have many more levels than this example shows,

Here is an example XML file.

<?xml version="1.0" encoding="UTF-8"?>
<ord:Order xmlns:ord="http://porterwebsites.com/Order"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://porterwebsites.com/Order Order.xsd">
	<ord:Header>
		<ord:HeaderId>1234</ord:HeaderId>
		<ord:HeaderName>ABC Company</ord:HeaderName>
	</ord:Header>
	<ord:Detail>
		<ord:Books>
			<ord:Book>
				<ord:Title>My Fabulous XML Book</ord:Title>
				<ord:Author>Jack</ord:Author>
			</ord:Book>
			<ord:Book>
				<ord:Title>My Big Book</ord:Title>
				<ord:Author>Sally</ord:Author>
			</ord:Book>
		</ord:Books>
	</ord:Detail>
</ord:Order>

Here is the XML schema definition (XSD).

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://porterwebsites.com/Order"
	xmlns:ord="http://porterwebsites.com/Order" elementFormDefault="qualified">

	<element name="Order" type="ord:Order" />

	<complexType name="Order">
		<sequence>
			<element name="Header" type="ord:Header" />
			<element name="Detail" type="ord:Detail" />
		</sequence>
	</complexType>
	<complexType name="Header">
		<sequence>
			<element name="HeaderId" type="ord:HeaderId" />
			<element name="HeaderName" type="ord:HeaderName" />
		</sequence>
	</complexType>
	<complexType name="Detail">
		<sequence>
			<element name="Books" type="ord:Books" />
		</sequence>
	</complexType>
	<complexType name="Books">
		<sequence>
			<element name="Book" type="ord:Book" maxOccurs="unbounded" />
		</sequence>
	</complexType>
	<complexType name="Book">
		<sequence>
			<element name="Title" type="ord:Title" />
			<element name="Author" type="ord:Author" />
		</sequence>
	</complexType>
	<!-- SIMPLE TYPES ARE LISTED BELOW -->
	<simpleType name="HeaderId">
		<restriction base="string">
			<maxLength value="15" />
		</restriction>
	</simpleType>
	<simpleType name="HeaderName">
		<restriction base="string">
			<maxLength value="15" />
		</restriction>
	</simpleType>
	<simpleType name="Title">
		<restriction base="string">
			<maxLength value="20" />
		</restriction>
	</simpleType>
	<simpleType name="Author">
		<restriction base="string">
			<maxLength value="15" />
		</restriction>
	</simpleType>
</schema>
Series Navigation<< XML Schema Definition Part 4XML Schema Definition Part 6 >>

Leave a comment

Your email address will not be published. Required fields are marked *