W3C home > Mailing lists > Public > xmlschema-dev@w3.org > October 2001

RE: constraining an element to be either simple or complex content

From: Priscilla Walmsley <priscilla@walmsley.com>
Date: Fri, 19 Oct 2001 12:24:44 -0400
To: "'Jeni Tennison'" <jeni@jenitennison.com>, "'Dolan, Kieran'" <DolanK@logica.com>
Cc: <xmlschema-dev@w3.org>
Message-ID: <004901c158ba$a317eca0$b92a6420@xmls>

> Unfortunately, this method isn't really much help unless you're able
> to define your own abstract type (so that the author of the instance
> is forced to use xsi:type) from which the types that you want to
> permit both derive. I don't think you can do this in this situation,
> though perhaps someone else can find a way.

You can't extend simple content to create complex content, but you _can_
restrict mixed complex content to create simple content.  In this situation,
you would define the types as:

<xs:complexType name="baseType" mixed="true" abstract="true">
  <xs:sequence>
    <xs:any minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="simpleType">
  <xs:simpleContent>
    <xs:restriction base="baseType">
      <xs:simpleType>
        <xs:restriction base="xs:integer"/>
      </xs:simpleType>
    </xs:restriction>
  </xs:simpleContent>
</xs:complexType>

<xs:complexType name="complexType">
  <xs:complexContent>
    <xs:restriction base="baseType">
      <xs:sequence>
        <xs:element name="Y" type="xs:integer" />
	</xs:sequence>
   </xs:restriction>
  </xs:complexContent>
</xs:complexType>

This is somewhat obscure but allowed by the Rec.

Like Jeni said, you would have to use xsi:type in the instance if you did
this.

Priscilla

-----------------------------------------------------------
Priscilla Walmsley                   priscilla@walmsley.com
Vitria Technology                     http://www.vitria.com
Author, Definitive XML Schema    (Prentice Hall, Dec. 2001)
-----------------------------------------------------------

> -----Original Message-----
> From: xmlschema-dev-request@w3.org
> [mailto:xmlschema-dev-request@w3.org]On Behalf Of Jeni Tennison
> Sent: Friday, October 19, 2001 11:56 AM
> To: Dolan, Kieran
> Cc: xmlschema-dev@w3.org
> Subject: Re: constraining an element to be either simple or complex
> content
>
>
> Hi Kieran,
>
> > Is there any way in XML schema to specify that an element contains
> > either simple content or complex content but not both?.
>
> Basically, no.
>
> > For example: <X>23</X> is valid and <X><Y>45</Y></X> is also valid
> > but not <X>23<Y>45</Y></X>. Use of a "mixed" content model permits
> > the first two cases but does not exclude the last one.
>
> You could go this route and add a schema adjunct (some RELAX NG or
> Schematron elements in xs:appinfo) that went on to define more tightly
> that the X element can either have text or a Y element as a child. Of
> course you would have to pull out these extra constraints yourself in
> order to test them, but they'd be there in the schema for
> documentation purposes.
>
> > I have tried to extend a complex type containing complex content
> > from a complex type containing simple content. XML Spy version 4.0
> > allows this but I understand from previous postings in this group
> > that this is not a correct XML schema construct.
>
> That's right - you're not allowed to extend a simple type with a
> complex type (Clause 1 of Schema Representation Constraint: Complex
> Type Definition Representation OK at
> http://www.w3.org/TR/xmlschema-1/#src-ct).
>
> Cheers,
>
> Jeni
>
> P.S.
>
> The only other thing that I can think of would be to use xsi:type
> on the instance document to indicate what type the X element should
> be. In the schema, define it as being xs:anyType:
>
> <xs:element name="X" type="xs:anyType" />
>
> and define types in your schema for the simple and complex types that
> you want, e.g.:
>
> <xs:simpleType name="simpleType">
>   <xs:restriction base="xs:integer" />
> </xs:simpleType>
>
> <xs:complexType name="complexType">
>   <xs:sequence>
>     <xs:element name="Y" type="xs:integer" />
>   </xs:sequence>
> </xs:complexType>
>
> and then use xsi:type to indicate the type of a particular X within
> the instance document:
>
>   <X xsi:type="simpleType">23</X>
>   <X xsi:type="complexType"><Y>45</Y></X>
>
> Unfortunately, this method isn't really much help unless you're able
> to define your own abstract type (so that the author of the instance
> is forced to use xsi:type) from which the types that you want to
> permit both derive. I don't think you can do this in this situation,
> though perhaps someone else can find a way.
>
> ---
> Jeni Tennison
> http://www.jenitennison.com/
>
>
Received on Friday, 19 October 2001 12:26:18 GMT

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