[Home] [By Thread] [By Date] [Recent Entries]

  • From: Roger L Costello <costello@m...>
  • To: "xml-dev@l..." <xml-dev@l...>
  • Date: Sat, 3 Apr 2021 13:03:57 +0000

Hi Folks,

As you know, a restaurant that offers a buffet style of dining lays out all foods on tables and then customers select the items they want. With a set menu style of dining the customer makes a choice and that determines all the items the customer will receive.

Should an XML Schema be written like a buffet: create a long list of optional elements and then let the instance document author select whichever ones he desires?

Or should an XML Schema be written like a set menu: there is a choice and once the instance document author selects a choice, that determines all the elements for that choice?

Example: Data for a magazine includes name of the magazine, publication date, cost, edition number. Data for a book includes title of the book, publication date, cost, category (fiction, non-fiction), binding (hardcover or softcover). Both magazines and books are publications. An XML Schema is created to model a publication. Here is a buffet style of designing the XML Schema:

<xs:element name="Publication">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="name" type="xs:string" minOccurs="0" />
            <xs:element name="publication-date" type="xs:date" minOccurs="0" />
            <xs:element name="cost" type="xs:string" minOccurs="0" />
            <xs:element name="edition-number" type="xs:nonNegativeInteger" minOccurs="0" />
            <xs:element name="title" type="xs:string" minOccurs="0" />
            <xs:element name="category" minOccurs="0">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="fiction" />
                        <xs:enumeration value="non-fiction" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="binding" minOccurs="0">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="hardcover" />
                        <xs:enumeration value="softcover" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="type">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="magazine" />
                    <xs:enumeration value="book" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>
</xs:element>

Notice that all the elements are optional (a buffet of elements!). All the elements for magazines and for books are declared within Publication.

Presumably, a person wanting to create an instance document for a magazine would select only the first four elements, e.g.,

<Publication type="magazine">
    <name>Popular Mechanics</name>
    <publication-date>1968-04-02</publication-date>
    <cost>25 cents</cost>
    <edition-number>430</edition-number>
</Publication>

The "type" attribute is used as a flag to indicate whether the person has selected elements appropriate for a magazine or for a book. In this case, type="magazine" indicates the intent is to provide data about a magazine.

Here is a set menu style of designing the XML Schema:

<xs:element name="Publication">
    <xs:complexType>
        <xs:choice>
            <xs:element name="magazine">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="name" type="xs:string" />
                        <xs:element name="publication-date" type="xs:date" />
                        <xs:element name="cost" type="xs:string" />
                        <xs:element name="edition-number" type="xs:nonNegativeInteger" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="book">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="title" type="xs:string" />
                        <xs:element name="publication-date" type="xs:date" />
                        <xs:element name="cost" type="xs:string" />
                        <xs:element name="category">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="fiction" />
                                    <xs:enumeration value="non-fiction" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="binding">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="hardcover" />
                                    <xs:enumeration value="softcover" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:choice>
    </xs:complexType>
</xs:element>

Notice that two choices are provided – magazine or book – and once a choice is made, all the elements for the choice are specified.

A person wanting to create an instance document for a magazine would select the first choice:

<Publication>
    <magazine>
        <name>Popular Mechanics</name>
        <publication-date>1968-04-02</publication-date>
        <cost>25 cents</cost>
        <edition-number>430</edition-number>
    </magazine>
</Publication>

The “type” attribute is gone and is replaced by an explicit element: <magazine> or <book>.

Okay, those are two ways to design an XML Schema. Which design do you prefer?

/Roger

 



[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member