W3C home > Mailing lists > Public > www-xml-schema-comments@w3.org > October to December 2000

Re: Substitution Groups?

From: C. M. Sperberg-McQueen <cmsmcq@acm.org>
Date: Tue, 26 Dec 2000 15:18:58 -0700
Message-Id: <>
To: "Nick K. Aghazarian" <Ace@AceProgrammer.com>
Cc: www-xml-schema-comments@w3.org
At 2000-12-08 12:20, Nick K. Aghazarian wrote:
>I've got element name="A" type="tA"
>I've got element name="B" extended from "A"
>I've got element name="C" extended from "B"
>I need to have an element name="X" that can be of type "A", "B" or "C".
>How can I do this?

I'm not sure I understand.  First, please let me know if
this paraphrase is correct.

You want to have four element types:  A, B, C, and X.

You want to have at least three complex types (tA, tB, tC),
which are the types of element types A, B, C respectively,
and which are derived from each other by extension (tC
extends tB extends tA).

And for element type X, you want to allow the instances to
be of type tA, tB, or tC.

If this is a correct paraphrase, you can do what you want,
using xsi:type on the instances of the X element type to
specify which complex type it should be validated against,
by declaring it of type tA.

Substitution groups don't enter into it.

A sample schema and document follow, illustrating the
scenario described above.  (N.B. element type A can also
take content of type tB and tC, in the same way that X

<!DOCTYPE schema PUBLIC "-//W3C//DTD XMLSCHEMA 200010//EN"
   targetNamespace = "file:///usr/local/namespaces/ace"
   xmlns:ace       = "file:///usr/local/namespaces/ace"
   xmlns           = "http://www.w3.org/2000/10/XMLSchema"
       This is a sample schema to demonstrate extensions and alternations
       in complex types.

<!--* Complex types tA, tB, tC have content of (i), (i,j), (i,j,k) *-->

<complexType name="tA">
<sequence><element type="integer" name="i"></element></sequence>

<complexType name="tB">
   <extension base="ace:tA">
    <sequence><element type="integer" name="j"></element></sequence>

<complexType name="tC">
   <extension base="ace:tB">
    <sequence><element type="integer" name="k"></element></sequence>

<!--* We define a top-level element to allow us to experiment with
     * all of the element types we define.  Call it 'ace'.
<element name="ace">
   <choice maxOccurs="unbounded" minOccurs="0">
    <element ref="ace:A"></element>
    <element ref="ace:B"></element>
    <element ref="ace:C"></element>
    <element ref="ace:X"></element>

<!--* Global element types A, B, C are of type tA, tB, tC *-->
<element type="ace:tA" name="A"></element>
<element type="ace:tB" name="B"></element>
<element type="ace:tC" name="C"></element>

<!--* Global element type X is of type tA. *-->

<element type="ace:tA" name="X"></element>


Here is a sample document instance, with three legal and three
illegal X elements:

<!--* These are OK *-->
<X xsi:type="tA"><i>123</i></X>
<X xsi:type="tB"><i>123</i><j>456</j></X>
<X xsi:type="tC"><i>123</i><j>456</j><k>789</k></X>

<!--* These are not OK *-->
<X xsi:type="tB"><i>123</i></X>
<X xsi:type="tC"><i>123</i><j>456</j></X>
<X xsi:type="tA"><i>123</i><j>456</j><k>789</k></X>

I hope this helps.  If it's not what you are looking for,
let me know.

-C. M. Sperberg-McQueen
Received on Tuesday, 26 December 2000 17:18:09 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:08:50 UTC