Advertisement:

Skystone Software

http://www.SkystoneSoftware.com

Skystone Software

Skystone Software is an Echosoft Design Studio, dedicated to the rapid design and development of high-end enterprise application systems built within the Microsoft .NET framework. A software consultancy serving clients primarily in the Hartford, Connecticut area of the United States, Skystone provides rapid and affordable software development and project management.


Read More

Scott Waletzko

Microsoft MVP Scott Waletzko, founder of Skystone Software, is a Windows and Web programmer and a Microsoft Most Valuable Professional (Visual Developer - Visual Basic) since 2006. Scott has extensive professional experience designing and developing software using C#, Visual Basic (both COM and .NET), HTML, JavaScript, and Flash / ActionScript.


Scott's Resume Scott's Blog

Scott Waletzko's Blog - Latest Entry
Xml Schema - Any Number of Elements in any Order
Published: 10/9/2008
XMl / RSS

Xml Schema is a powerfule tool that allows data designers to develop their own markup languages (like HTML) using a complex set of definitions and restrictions. It's not, however, always straight forward how the simplest things can be achieved using Xsd. Take, for example, the definition of a subset of elements that can exist beneath a parent element in any order, any combination. Xml Schema provides the xs:sequence, xs:all, and xs:choice elements to allow designers to specify groups of subelements, but it's nto obvious by looking at their descriptions which would facilitate this particular design. Let's look at each in turn:

  • xs:sequence
    Specifies a specific set of sub elements that should appear in a specific order.
  • xs:all
    Specifies that any of the elements defined can appear in any order zero or one times.
  • xs:choice
    Specifies that only one of the elements defined will appear.

So at first look, none of these will allow us to design a subset of elements that can appear in any order, each element appearing any number of times. The key to cracking this puzzle, however, lies with the attributes that can be defined for each of the above elements in the schema document. xs:choice has a "minOccurs" and "maxOccurs" attribute, which basically tells the validating reader that the choice itself can occur more than once. This allows us to specify any number of elements in any order, as follows:

Xml Schema:

<xs:schema id="test" targetNamespace="http://tempuri.org/test.xsd">
	<xs:complexType name="testType">
		<xs:choice minOccurs="0" maxOccurs="unbounded">
			<xs:element name="test1" />
			<xs:element name="test2" />
			<xs:element name="test3" />
		</xs:choice>
	</xs:complexType>
	<xs:element name="testType" type="testType" />
</xs:schema>
			

Sample Xml:

<testType xmlns="http://tempuri.org/test.xsd">
	<test3 />
	<test2 />
	<test1 />
	<test2 />
	<test2 />
	<test2 />
	<test3 />
	<test2 />
	<test1 />
</testType>			
			

Scott's Blog