[XMLSCHEMA-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: elements with the same name

From: Eddie Robertsson <eddie@allette.com.au>
Date: Wed, 29 Aug 2001 09:56:43 +1000
Message-ID: <3B8C2FBB.18483EEC@allette.com.au>
To: D Gross <justdave@onebox.com>
CC: xmlschema-dev@w3.org
xsd same name
> I would do it like so:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" elementFormDefault="qualified">
>  <xsd:element name="top">
>   <xsd:complexType>
>    <xsd:sequence>
>     <xsd:element name="Address" type="US"/>
>     <xsd:element name="Address" type="China"/>
>    </xsd:sequence>
>   </xsd:complexType>
>  </xsd:element>
>  <xsd:complexType name="US">
>   <xsd:sequence>
>    <xsd:element name="Street" type="xsd:string"/>
>    <xsd:element name="City" type="xsd:string"/>
>    <xsd:element name="Zip" type="xsd:string"/>
>   </xsd:sequence>
>  </xsd:complexType>
>  <xsd:complexType name="China">
>   <xsd:sequence>
>    <xsd:element name="Street" type="xsd:string"/>
>    <xsd:element name="City" type="xsd:string"/>
>    <xsd:element name="Province" type="xsd:string"/>
>   </xsd:sequence>
>  </xsd:complexType>
> </xsd:schema>

This is not a valid schema. You can't have two elements with the same name (Address) in the
same content model with different types (also it's using the old CR namespace). Instead of the
above solution you would create an abstract complexType to hold the common element in the
different Address types like:

<xsd:complexType name="baseAddressType" abstract="true">
   <xsd:sequence>
      <xsd:element name="Street" type="xs:string"/>
      <xsd:element name="City" type="xs:string"/>
   </xsd:sequence>
</xsd:complexType>

This is the type you will use for the Address element:

<xsd:element name="top">
   <xsd:complexType>
      <xsd:sequence>
         <xsd:element name="Address" type="baseAddressType" minOccurs="2" maxOccurs="2"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:element>

The you create your specific address types by extending the baseAddressType. The complete
schema would look like:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
 <xsd:element name="top">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="Address" type="baseAddressType" minOccurs="2" maxOccurs="2"/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
 <xsd:complexType name="baseAddressType" abstract="true">
  <xsd:sequence>
   <xsd:element name="Street" type="xsd:string"/>
   <xsd:element name="City" type="xsd:string"/>
  </xsd:sequence>
 </xsd:complexType>
 <xsd:complexType name="US">
  <xsd:complexContent>
   <xsd:extension base="baseAddressType">
    <xsd:sequence>
     <xsd:element name="Zip" type="xsd:string"/>
    </xsd:sequence>
   </xsd:extension>
  </xsd:complexContent>
 </xsd:complexType>
 <xsd:complexType name="China">
  <xsd:complexContent>
   <xsd:extension base="baseAddressType">
    <xsd:sequence>
     <xsd:element name="Province" type="xsd:string"/>
    </xsd:sequence>
   </xsd:extension>
  </xsd:complexContent>
 </xsd:complexType>
</xsd:schema>

The instance document would stay the same but change the namespace to the Rec version
(http://www.w3.org/2001/XMLSchema-instance)

Cheers,
/Eddie

> Then your XML would look like this:
> <top xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
>  <Address xsi:type="US">
>   <Street>12345 main street</Street>
>   <City>JoeTown</City>
>   <Zip>2456</Zip>
>  </Address>
>  <Address xsi:type="China">
>   <Street>12344 lo pai street</Street>
>   <City>...</City>
>   <Province>..</Province>
>  </Address>
> </top>
Received on Tuesday, 28 August 2001 19:57:50 GMT

Subscribe to the Stylus Scoop newsletter for helpful XML tips and tutorials.
Email
First Name
Last Name
Company

Download Stylus Studio 6 XML Enterprise Edition

Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.