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: <4.3.2.7.1.20001226122957.028c2ed8@espanola.com>
To: "Nick K. Aghazarian" <Ace@AceProgrammer.com>
Cc: www-xml-schema-comments@w3.org
At 2000-12-08 12:20, Nick K. Aghazarian wrote:
>Hello,
>
>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
can.)

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

<!--* 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>

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

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

<!--* 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">
  <complexType>
   <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>
   </choice>
  </complexType>
</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>

</schema>
----------

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

----------
<ace
   xmlns="file:///usr/local/namespaces/ace"
   xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
 >
<!--* These are OK *-->
<A><i>123</i></A>
<B><i>123</i><j>456</j></B>
<C><i>123</i><j>456</j><k>789</k></C>
<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>
</ace>
----------

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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Sunday, 6 December 2009 18:12:49 GMT