Re: Include multiple complex types into one element ...

> I'm new to XSD and was wondering if I could include multiple complex types
> into one element.  For example, I have two complex types NameType and
> AddressType.
>
> =====
>
>   <xs:complexType name="NameType">
>     <xs:sequence>
>       <xs:element name="FirstName" type="xs:string" />
>       <xs:element name="LastName" type="xs:string" />
>     </xs:sequence>
>   </xs:complexType>
>
>   <xs:complexType name="AddressType">
>     <xs:sequence>
>       <xs:element name="Country" type="xs:string" />
>       <xs:element name="Zip" type="xs:string" />
>     </xs:sequence>
>   </xs:complexType>


As you're new here's a schema which should demonstrate a few things to
get you going.

First it's in the "Garden of Eden" style which is great name for
something as simple as defining all of the elements and types globally
- notice how all of the elements are defined first at the top, then
the simple types and groups, then the complex types.  No definition is
anonymous (hidden within another definition).   This is by far the
best way of writing your schema.

Next notice how the names are defined as "non-empty-string" - this is
a custom simple type because the inbuilt "xs:string" type allows the
empty string (both " " and "").... which is usually not what you need.
 Countries are defined using an enumeration, and the Zip code is a
regex.

Groups are fairly straightforward, allowing you to reuse blocks of
elements, and the complexType root is just those groups one after the
other.  You don't have to use the groups here - you could just
reference the four elements directly within the "root" type and they
must occur in that order...


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

  <xs:element name="root" type="root"/>
  <xs:element name="FirstName" type="non-empty-string"/>
  <xs:element name="LastName" type="non-empty-string"/>
  <xs:element name="Country" type="country"/>
  <xs:element name="Zip" type="zip"/>

  <xs:simpleType name="country">
		<xs:restriction base="xs:string">
			<xs:enumeration value="Cornwall"/>
			<xs:enumeration value="United Kingdom"/>
			<xs:enumeration value="Canada"/>
		</xs:restriction>
	</xs:simpleType>
	
  <xs:simpleType name="zip">
	  <xs:restriction base="xs:string">
			<xs:pattern value="us zip code regex"/>
	  </xs:restriction>	
  </xs:simpleType>

  <xs:group name="nameGroup">
  	<xs:sequence>
		  <xs:element ref="FirstName"/>
		  <xs:element ref="LastName"/>	
	  </xs:sequence>
  </xs:group>
  	
  <xs:group name="addressGroup">
  	<xs:sequence>
		  <xs:element ref="Country"/>
		  <xs:element ref="Zip"/>	
	  </xs:sequence>
  </xs:group>

  <xs:complexType name="root">
	    <xs:sequence>
		      <xs:group ref="nameGroup"/>
		      <xs:group ref="addressGroup"/>
	    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="non-empty-string">
	    <xs:restriction base="xs:string">
		      <xs:whiteSpace value="collapse"/>
		      <xs:minLength value="1"/>
	    </xs:restriction>
  </xs:simpleType>
</xs:schema>


hopefully this is helpful...

-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Received on Wednesday, 9 July 2008 09:12:24 UTC