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

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

From: Ashok Malhotra <ashokma@microsoft.com>
Date: Fri, 19 Oct 2001 09:53:53 -0700
Message-ID: <E5B814702B65CB4DA51644580E4853FB01040573@red-msg-12.redmond.corp.microsoft.com>
To: "Jeni Tennison" <jeni@jenitennison.com>, "Dolan, Kieran" <DolanK@logica.com>
Cc: <xmlschema-dev@w3.org>
> Is there any way in XML schema to specify that an element contains 
> either simple content or complex content but not both?.

Create a type that contains a choice.  Let one of the choice subelements
have simple content and the other complex context.  Granted, this is one
level deeper than you probably want but it'll do the job.

All the best, Ashok 
===========================================================
Ashok Malhotra              <mailto: ashokma@microsoft.com> 
Microsoft Corporation
212 Hessian Hills Road
Croton-On-Hudson, NY 10520 USA 
Redmond: 425-703-9462                New York: 914-271-6477 



-----Original Message-----
From: Jeni Tennison [mailto:jeni@jenitennison.com] 
Sent: Friday, October 19, 2001 8: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 13:17:45 GMT

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