Nested XML and XML - schema definition

654 Views Asked by At

I want to define the XML schema for an XML representation of shopping order data.

See following requirements

  • The XML representation of order should contain order id, description, request date, and many line item
  • Each line item element should contain book, quantity, and price. Price (which is price per book) should be restricted to values greater than 0
  • Each book should contain book id, book name, genre, publish date, and many authors. Genre should be restricted so that it may contain only the following values {science-fiction, mystery, thriller, drama}
  • Each author should contain bio,last name, first name, and optional pen name.

Evaluation Criteria

  • allows many line items
  • allows many authors for a book and have an OPTIONAL pen name
  • allows to specify price and restricts to values greater than 0
  • allows to specify genre but restricts the list of values to be EXACTLY that state above. Please someone help me figure out XML definition for this type of nested requirements.

here's my code

XML

<?xml version="1.0" encoding="UTF-8" ?>

<orders>
    <order id="1">
        <description>first order of the day</description>
        <req_date>9-4-2020</req_date>
        <line_items>
            <book>
                <id>1</id>
                <name>Book1</name>
                <publish_date>12-2-2010</publish_date>
                <genre>science-fiction</genre>
                <authors>
                    <author id="120">
                        <bio> description about author120</bio>
                        <first_name>George</first_name>
                        <last_name>Smith</last_name>
                        <pen_name>pen name </pen_name>
                    </author>
                    <author id="122">
                        <bio> description about author122</bio>
                        <first_name>George</first_name>
                        <last_name>Smith</last_name>
                    </author>
                </authors>
            </book>
            <book>
                <id>1</id>
                <name>Book2</name>
                <publish_date>12-2-2010</publish_date>
                <genre>science-fiction</genre>
                <authors>
                    <author id="120">
                        <bio> description about author120</bio>
                        <first_name>George</first_name>
                        <last_name>Smith</last_name>
                        <pen_name>pen name </pen_name>
                    </author>
                </authors>
            </book>
            <quantity>1</quantity>
            <price>20</price>
        </line_items>
    </order>
</orders>

XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="orders">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="order">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:string" name="description" />
                            <xs:element type="xs:string" name="req_date" />
                            <!-- criteria 1 -->
                            <xs:element name="line_items" minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="book" maxOccurs="unbounded" minOccurs="0">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element type="xs:byte" name="id" />
                                                    <xs:element type="xs:string" name="name" />
                                                    <xs:element type="xs:string" name="publish_date" />
                                                    <xs:element name="genre" >
                                                        <xs:simpleType>
                                                            <xs:restriction base="xs:string">
                                                                <xs:enumeration value="science-fiction"/>
                                                                <xs:enumeration value="mystery" />
                                                                <xs:enumeration value="thriller" />
                                                                <xs:enumeration value="drama" />

                                                            </xs:restriction>
                                                        </xs:simpleType>
                                                    </xs:element>
                                                    <!-- criteria 2 -->
                                                    <xs:element name="authors" minOccurs="1" maxOccurs="unbounded">
                                                        <xs:complexType>
                                                            <xs:sequence>
                                                                <xs:element name="author" maxOccurs="unbounded" minOccurs="0">
                                                                    <xs:complexType>
                                                                        <xs:sequence>
                                                                            <xs:element type="xs:string" name="bio" />
                                                                            <xs:element type="xs:string" name="first_name" />
                                                                            <xs:element type="xs:string" name="last_name" />
                                                                            <!-- make a pen name optional -->
                                                                            <xs:element type="xs:string" name="pen_name" minOccurs="0" />
                                                                        </xs:sequence>
                                                                        <xs:attribute type="xs:byte" name="id" use="optional" />
                                                                    </xs:complexType>
                                                                </xs:element>
                                                            </xs:sequence>
                                                        </xs:complexType>
                                                    </xs:element>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                        <xs:element type="xs:byte" name="quantity" />
                                        <!-- price criteria -->
                                        <xs:element name="price">
                                        <xs:simpleType>
                                            <xs:restriction base="xs:integer">
                                                <xs:minInclusive value="0"/>
                                            </xs:restriction>
                                        </xs:simpleType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute type="xs:byte" name="id" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Suggest me If my approach is working? and Suggest me If I'm missing something?

0

There are 0 best solutions below