W3C home > Mailing lists > Public > xmlschema-dev@w3.org > March 2003

Re: Generic Question

From: Shirish Kulkarni <shirish_kul@yahoo.com>
Date: Tue, 4 Mar 2003 03:22:17 -0800 (PST)
Message-ID: <20030304112217.58243.qmail@web41104.mail.yahoo.com>
To: Jeni Tennison <jeni@jenitennison.com>
Cc: xmlschema-dev@w3.org

Thanks Jeni for a quick and informative response. 

Another dimension to the problem is when I would not
like to have the Type1 or Type2 exposed to the
instance document ( but a common type "TopType" is
fine ) and still able to switch the validation to the
required type internally. 

a.xml =>
<Node xsi:type="TopType">
  <Sub1>
  <Sub2>
  <Sub3>
</Node>

b.xml =>
<Node xsi:type="TopType">
  <Sub4>
  <Sub5>
</Node>

and within Schema I will have 
c.xsd => 
<xs:complexType name="BaseType" abstract="true" />

<xs:complexType name="Type1">
  <xs:complexContent>
    <xs:extension base="BaseType">
      <xs:sequence>
        <xs:element name="Sub1" type="xsd:string"/>
        <xs:element name="Sub2" type="xsd:string"/>
        <xs:element name="Sub3" type="xsd:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="Type2">
  <xs:complexContent>
    <xs:extension base="BaseType">
      <xs:sequence>
        <xs:element name="Sub4" type="xsd:string"/>
        <xs:element name="Sub5" type="xsd:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="TopType">
  <xs:complexContent>
    <xs:extension base="Type1">
    </xs:extension>
    <xs:extension base="Type2">
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

Is it feasible to have a type getting extension from
two types ? Would this switch the validation to types
to Type1 & Type2 on the basis of the contents of the
<Node> element ? 

With best regards,
Shirish 


--- Jeni Tennison <jeni@jenitennison.com> wrote:
> Hi Shirish,
> 
> > I would like to have an element to have a type,
> which is a choices
> > between number of types.
> 
> The only way the same element can take on different
> complex types in
> XML Schema is if the types are related. You then
> have to switch
> between the types using the xsi:type attribute on
> the element in the
> instance document. For example:
> 
> a.xml =>
> <Node xsi:type="Type1">
>   <Sub1>
>   <Sub2>
>   <Sub3>
> </Node>
> 
> b.xml =>
> <Node xsi:type="Type2">
>   <Sub4>
>   <Sub5>
> </Node>
> 
> Your types are related already, since they are both
> restrictions of
> xs:anyType, so you could just use the declaration:
> 
> <xs:element name="Node" type="xs:anyType" />
> 
> but this would mean that the element could contain
> anything (unless
> the xsi:type attributes were present). So it's
> probably best to create
> an anonymous abstract type from which the two types
> can be derived by
> extension:
> 
> <xs:complexType name="BaseType" abstract="true" />
> 
> <xs:complexType name="Type1">
>   <xs:complexContent>
>     <xs:extension base="BaseType">
>       <xs:sequence>
>         <xs:element name="Sub1" type="xsd:string"/>
>         <xs:element name="Sub2" type="xsd:string"/>
>         <xs:element name="Sub3" type="xsd:string"/>
>       </xs:sequence>
>     </xs:extension>
>   </xs:complexContent>
> </xs:complexType>
> 
> <xs:complexType name="Type2">
>   <xs:complexContent>
>     <xs:extension base="BaseType">
>       <xs:sequence>
>         <xs:element name="Sub4" type="xsd:string"/>
>         <xs:element name="Sub5" type="xsd:string"/>
>       </xs:sequence>
>     </xs:extension>
>   </xs:complexContent>
> </xs:complexType>
> 
> You can then declare the element to be of that
> abstract base type:
> 
> <xs:element name="Node" type="BaseType" />
> 
> Cheers,
> 
> Jeni
> 
> ---
> Jeni Tennison
> http://www.jenitennison.com/
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/
Received on Tuesday, 4 March 2003 06:22:49 GMT

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