W3C home > Mailing lists > Public > xmlschema-dev@w3.org > June 2012

Example of an identity constraint that is not applied to substitution elements?

From: Costello, Roger L. <costello@mitre.org>
Date: Fri, 15 Jun 2012 17:48:07 +0000
To: "xmlschema-dev@w3.org" <xmlschema-dev@w3.org>
Message-ID: <B5FEE00B53CF054AA8439027E8FE17751920AD3B@IMCMBX04.MITRE.ORG>
Hi Folks,

In section 2.2.4.4 of the Structures specification it says:

    ... elements substitutable for E are required to 
    have types derived from T, but are not required 
    to enforce the identity constraints ... of E.

I am trying to construct an example to illustrate this. That is, I seek to construct an element that has identity constraints and has substitutable elements, and the identity constraints are not enforced on the substitution elements.

Here's what I tried:

A BookStore contains multiple Books and each Book has a Title, Author, Date, ISBN, and Publisher. xsd:key is used to constrain each ISBN  to be unique:
-----------------------------------------------------------------------------------
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     targetNamespace="http://www.books.org"
                    xmlns="http://www.books.org"
                    xmlns:bk="http://www.books.org"
                    elementFormDefault="qualified">

    <xsd:element name="BookStore">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Book" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="Title" type="xsd:string" />
                            <xsd:element name="Author" type="xsd:string" />
                            <xsd:element name="Date" type="xsd:string" />
                            <xsd:element ref="ISBN" />
                            <xsd:element name="Publisher" type="xsd:string" />
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:key name="PK">
            <xsd:selector xpath="bk:Book"/>
            <xsd:field xpath="bk:ISBN"/>
        </xsd:key>
    </xsd:element>

    <xsd:element name="ISBN" type="xsd:string" />
    <xsd:element name="ISBN-10" substitutionGroup="ISBN" />
    <xsd:element name="ISBN-13" substitutionGroup="ISBN" />

</xsd:schema>
-----------------------------------------------------------------------------------
Notice the xsd:key requires the ISBN element be unique.

Also notice that I declared ISBN globally, and declared ISBN-10 and ISBN-13 to be substitutable for ISBN.


Here is an instance document that has each ISBN value unique and is schema-valid:

<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation=
                                  "http://www.books.org
                                   BookStore.xsd">
        <Book>
                <Title>My Life and Times</Title>
                <Author>Paul McCartney</Author>
                <Date>1998</Date>
                <ISBN>1-56592-235-2</ISBN>
                <Publisher>McMillin Publishing</Publisher>
        </Book>
        <Book>
                <Title>Illusions The Adventures of a Reluctant Messiah</Title>
                <Author>Richard Bach</Author>
                <Date>1977</Date>
                <ISBN>0-440-34319-4</ISBN>
                <Publisher>Dell Publishing Co.</Publisher>
        </Book>
        <Book>
                <Title>The First and Last Freedom</Title>
                <Author>J. Krishnamurti</Author>
                <Date>1954</Date>
                <ISBN>0-06-064831-7</ISBN>
                <Publisher>Harper &amp; Row</Publisher>
        </Book>
</BookStore>

Here is the same instance document, except I appended a copy of the first book, replacing its ISBN element with an ISBN-10 element:

<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation=
                                  "http://www.books.org
                                   BookStore.xsd">
        <Book>
                <Title>My Life and Times</Title>
                <Author>Paul McCartney</Author>
                <Date>1998</Date>
                <ISBN>1-56592-235-2</ISBN>
                <Publisher>McMillin Publishing</Publisher>
        </Book>
        <Book>
                <Title>Illusions The Adventures of a Reluctant Messiah</Title>
                <Author>Richard Bach</Author>
                <Date>1977</Date>
                <ISBN>0-440-34319-4</ISBN>
                <Publisher>Dell Publishing Co.</Publisher>
        </Book>
        <Book>
                <Title>The First and Last Freedom</Title>
                <Author>J. Krishnamurti</Author>
                <Date>1954</Date>
                <ISBN>0-06-064831-7</ISBN>
                <Publisher>Harper &amp; Row</Publisher>
        </Book>
        <Book>
                <Title>My Life and Times</Title>
                <Author>Paul McCartney</Author>
                <Date>1998</Date>
                <ISBN-10>1-56592-235-2</ISBN-10>
                <Publisher>McMillin Publishing</Publisher>
        </Book>
</BookStore>

That generates this error, 

           The field in constraint {PK} has no value.

That's not the problem that I am trying to illustrate. 

I seek a way to use an ISBN-10 element, containing a value that duplicates one of the ISBN element values, and does NOT generate an error.

But how?

/Roger
Received on Friday, 15 June 2012 17:48:36 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 14:56:20 UTC