W3C home > Mailing lists > Public > xmlschema-dev@w3.org > July 2008

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

From: Andrew Welch <andrew.j.welch@gmail.com>
Date: Wed, 9 Jul 2008 10:11:45 +0100
Message-ID: <74a894af0807090211q756b7dadu837fdd0320302b30@mail.gmail.com>
To: "Dragon Fly" <dragon-fly999@hotmail.com>
Cc: xmlschema-dev@w3.org

> 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

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"

  <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:simpleType name="zip">
	  <xs:restriction base="xs:string">
			<xs:pattern value="us zip code regex"/>

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

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

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

hopefully this is helpful...

Andrew Welch
Kernow: http://kernowforsaxon.sf.net/
Received on Wednesday, 9 July 2008 09:12:24 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:14 UTC