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

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

From: Dragon Fly <dragon-fly999@hotmail.com>
Date: Wed, 9 Jul 2008 08:01:08 -0400
Message-ID: <BLU119-W2484CC63044C1FF80BB694B9960@phx.gbl>
To: Andrew Welch <andrew.j.welch@gmail.com>
CC: <xmlschema-dev@w3.org>
Thank you both for the information.  Andrew, what is the benefit of defining a "simple" element globally? For example, the nameGroup can be defined as follows (i.e. no refs are used for the FirstName and LastName elements).

<xs:group name="nameGroup">
  <xs:sequence>
    <xs:element name="FirstName" type="non-empty-string"/>
    <xs:element name="LastName" type="non-empty-string"/>
  </xs:sequence>
</xs:group>

> Date: Wed, 9 Jul 2008 10:11:45 +0100
> From: andrew.j.welch@gmail.com
> To: dragon-fly999@hotmail.com
> CC: xmlschema-dev@w3.org
> Subject: 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/
> 

_________________________________________________________________
Itís a talkathon Ė but itís not just talk.
http://www.imtalkathon.com/?source=EML_WLH_Talkathon_JustTalk
Received on Wednesday, 9 July 2008 12:01:50 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:15:05 GMT