[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

XML Schema design: buffet or set menu?

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

XML Schema design: buffet or set menu?

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]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.