OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: XML Schema and context-senstive datatyping

[ Lists Home | Date Index | Thread Index ]
  • From: ht@cogsci.ed.ac.uk (Henry S. Thompson)
  • To: Sam Hunting <sam_hunting@yahoo.com>
  • Date: Thu, 05 Oct 2000 12:06:42 +0100

Sam Hunting <sam_hunting@yahoo.com> writes:

> If this is a FAQ I apologize in advance.
>  
> Suppose I have an address that can look like this:
>  
>     <Address>
>         <PostalCode>12345-6789</PostalCode>
>         <CountryCode>US</CountryCode>
>     </Address>
>  
> but can also look like this:
>  
>     <Address>
>         <PostalCode>H3H-2A6</PostalCode>
>         <CountryCode>CA</CountryCode>
>     </Address>
>  
> and I want to change the datatype of <PostalCode> based on the value of
> <CountryCode> so I can validate the user's entries, can I do that using
> XML Schema? If so, how?
>  
> (One would wish to validate a Zip code for the USA, but a Canadian
> Postal Code for Canada. The dataype of country code would be an
> enumerated list a la ISO 3166.

The substitution group [1] mechanism is suitable for satisfying this
sort of requirement, as follows:

<element name="Address" abstract="true" type="Address">

<group name="coreAddr">
  <element name="Addressee"/>
  <element name="Street1" minOccurs="0"/>
  ...
</group>

<simpleType name="CCodes">
 <restriction base="NMTOKEN">
  <enumeration value="US"/>
  <enumeration value="UK"/>
  <enumeration value="CA"/>
  . . .
 </restriction>
</simpleType>

<complexType name="Address">
 <sequence>
  <group ref="coreAddr"/>
  <element name="PostalCode"/>
  <element name="CountryCode" type="CCodes"/>
 </sequence>
</complexType>

<element name="USAddress" substitutionGroup="Address">
 <complexType>
  <complexContent>
   <restriction base="Address">
    <sequence>
     <group ref="coreAddr"/>
     <element name="PostalCode" type="USPostalCode"/>
     <element name="CountryCode">
      <simpleType>
       <restriction base="CCodes">
        <enumeration value="US"/>
       </restriction>
     </element>
    </sequence>
   </restriction>
  </complexContent>
 </complexType>
</element>

<element name="CAAddress" substitutionGroup="Address">
 <complexType>
  <complexContent>
   <restriction base="Address">
    <sequence>
     <group ref="coreAddr"/>
     <element name="PostalCode" type="CAPostalCode"/>
     <element name="CountryCode">
      <simpleType>
       <restriction base="CCodes">
        <enumeration value="CA"/>
       </restriction>
     </element>
    </sequence>
   </restriction>
  </complexContent>
 </complexType>
</element>

I leave as an exercise to the reader the definitions of the simple
types CAPostalCode and USPostalCode.

The above of course requires some application-level redundancy.  I've
expressed that with element names:

    <USAddress>
        <PostalCode>12345-6789</PostalCode>
        <CountryCode>US</CountryCode>
    </USAddress>
 
vs.
 
    <CAAddress>
        <PostalCode>H3H-2A6</PostalCode>
        <CountryCode>CA</CountryCode>
    </CAAddress>

An alternative is to name the restricted types above in the obvious
and do this instead:

    <Address xsi:type='USAddress'>
        <PostalCode>12345-6789</PostalCode>
        <CountryCode>US</CountryCode>
    </Address>
 
vs.

    <Address xsi:type='CAAddress'>
        <PostalCode>H3H-2A6</PostalCode>
        <CountryCode>CA</CountryCode>
    </Address>

ht

[1] http://www.w3.org/TR/xmlschema-1/#Element_Equivalence_Class
-- 
  Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
          W3C Fellow 1999--2001, part-time member of W3C Team
     2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
	    Fax: (44) 131 650-4587, e-mail: ht@cogsci.ed.ac.uk
		     URL: http://www.ltg.ed.ac.uk/~ht/




 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS