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.
|
|
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. |
|
Xml Schema - Any Number of Elements in any Order
Published: 10/9/2008 |
|
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:
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>