Welcome to MSDN Blogs Sign in | Join | Help

Galex's Semi-Structured Blog

SQL and XML in SQL Server 2005
Content models and complex types

Today, I want to a little about how content models are defined in complex types. These are important concepts in not only creating schemas and instances that are valid against the schemas, but will also help in understanding static typing and static typing errors within XQuery (this will be topic in a couple of posts).

The important thing to understand about complex types is that they define content models that contain elements and attributes. The structure and potentially the order of elements within a complex type is the content model. XSD allows you group your content that specifies if all the elements are required and should appear in order in a valid instance. These content groups and elements within these content groups can be specified as optional and may even repeat an arbitrary number of times.

A sequence group specifies that the content should all be present and in order. A choice group specifies that one item in the group should be present. An all group specifies that all the content should be present, but in any order. Sequences and choice groups can be nested within each other. An all content group can only be at the top of a complex content model.

Here is an example:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <
xs:complexType name="foo"
>
      <
xs:sequence maxOccurs="unbounded"
>
         <
xs:element name="foo" type="xs:string"
/>
         <
xs:element name="bar" type="xs:int" minOccurs="0"
/>
         <
xs:choice minOccurs="0"
>
            <
xs:element name="baz" type="xs:double"
/>
            <
xs:sequence
>
               <
xs:element name="foo2" type="xs:dateTime"
/>
               <
xs:element name="bar2" type="xs:decimal"
/>
            </
xs:sequence
>
         </
xs:choice
>
      </
xs:sequence
>
   </
xs:complexType
>

   <xs:complexType name="bar">
      <
xs:all
>
         <
xs:element name="foo" type="xs:string"
/>
         <
xs:element name="bar" type="xs:string" minOccurs="0"
/>
      </
xs:all
>
   </
xs:complexType
>
</
xs:schema
>

The complex type "foo" is defined as a sequence that has an element foo followed by bar and finally a choice between an element baz and another sequence containing foo2 and bar2. And, the complex type "bar" is defined an all content that requires elements foo and bar in any order.

Notice that I can specify minOccurs and maxOccurs attributes on model groups and elements. For the complex type "foo", the sequence can be repeat an arbitrary number of times (maxOccurs="unbounded". And the element bar within this sequence does not have to appear at all within one instance of the sequence (minOccurs="0"). The same goes for the choice model group.

Posted: Monday, October 10, 2005 10:44 AM by galexy

Comments

No Comments

New Comments to this post are disabled
Page view tracker