Re: Generic Question

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 UTC