1

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?

  • Please post what you have created so far and what specific problem you are facing. – Chris Apr 10 '20 at 10:17
  • I created XML document with order tag then book and author but I don't know how to merge them to meet the requirements –  Apr 10 '20 at 10:22
  • 2
    Creating a document is the best first step towards creating a schema, in fact some IDEs such as IntelliJ will generate an initial schema based on a document that you can then edit. Main thing is to read documentation about XML schema to ensure you fully understand and are able to correctly edit to what you need. If you are looking for answers on stack overflow you can't really just post requirements you have been given, you need to show us what you have produced so far and what help you need in order to move forward from there – Chris Apr 10 '20 at 12:25
  • First looking at your XML doc, the only thing I'd more is that I think from the description that instead of line_items containing multiple books you would have multiple occurences of line_item, each consisting of one book and quantity and price - though I note in your schema you do have line_items as unbounded. – Chris Apr 10 '20 at 14:20
  • 1
    And looking at the schema, without cross checking all your requirements it looks like you're going in the right direction, though note that price has to be greater than 0. Next step when you are happy with your XML doc is to validate it against your schema to check they match up. You could write a quick program to do that, or use an IDE, or there are online validation tools eg https://www.xmlvalidation.com/ – Chris Apr 10 '20 at 14:28
  • Is this an assignment? If so, good luck – Chris Apr 10 '20 at 14:31
  • I have validated the schema and xml . I'm not getting your point about `line_items`. @Chris –  Apr 10 '20 at 14:33
  • It's about singular and plural names - see where the requirement says a book can have many authors and so you have multiple elements called author (singular). In the same way, if an order has many line_items you would have multiple elements called line_item (singular). The other point is my interpretation of the requirement. I think a line item only contains a single book. – Chris Apr 10 '20 at 14:36

0 Answers0