Create an xml for the bookstore. Validate the same using both DTD and XSD.


1. Bookstore XML Document (bookstore.xml)


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

<!DOCTYPE bookstore SYSTEM "bookstore.dtd">

<!-- <?xml-stylesheet type="text/xsl" href="bookstore.xsl"?> -->

<!-- For XSD validation, replace the DOCTYPE with: 

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

<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

           xsi:noNamespaceSchemaLocation="bookstore.xsd">

-->


<bookstore>

    <book id="bk101" category="fiction" format="paperback">

        <title>The Great Gatsby</title>

        <author>

            <first-name>F. Scott</first-name>

            <last-name>Fitzgerald</last-name>

        </author>

        <year>1925</year>

        <price currency="USD">10.99</price>

        <isbn>9780743273565</isbn>

        <publisher>Scribner</publisher>

        <stock>

            <quantity>45</quantity>

            <location>Aisle 3, Shelf B</location>

        </stock>

        <description>Classic novel about the American Dream</description>

    </book>

    

    <book id="bk102" category="non-fiction" format="hardcover">

        <title>Sapiens: A Brief History of Humankind</title>

        <author>

            <first-name>Yuval Noah</first-name>

            <last-name>Harari</last-name>

        </author>

        <year>2011</year>

        <price currency="EUR">15.50</price>

        <isbn>9780062316097</isbn>

        <publisher>Harper</publisher>

        <stock>

            <quantity>28</quantity>

            <location>Aisle 5, Shelf A</location>

        </stock>

        <description>Exploration of human history and evolution</description>

    </book>

    

    <book id="bk103" category="fiction" format="ebook">

        <title>1984</title>

        <author>

            <first-name>George</first-name>

            <last-name>Orwell</last-name>

        </author>

        <year>1949</year>

        <price currency="GBP">8.99</price>

        <isbn>9780451524935</isbn>

        <publisher>Signet Classics</publisher>

        <stock>

            <quantity>0</quantity>

            <location>Out of Stock</location>

        </stock>

        <description>Dystopian social science fiction novel</description>

    </book>

    

    <book id="bk104" category="children" format="hardcover">

        <title>The Very Hungry Caterpillar</title>

        <author>

            <first-name>Eric</first-name>

            <last-name>Carle</last-name>

        </author>

        <year>1969</year>

        <price currency="USD">7.99</price>

        <isbn>9780399226908</isbn>

        <publisher>World Publishing Company</publisher>

        <stock>

            <quantity>62</quantity>

            <location>Aisle 1, Shelf C</location>

        </stock>

        <description>Children's picture book</description>

    </book>

</bookstore>



2. DTD Validation (bookstore.dtd)





<!-- bookstore.dtd -->

<!ELEMENT bookstore (book+)>

<!ELEMENT book (title, author, year, price, isbn, publisher, stock, description)>

<!ATTLIST book

    id ID #REQUIRED

    category (fiction|non-fiction|children|science|biography|history) #REQUIRED

    format (paperback|hardcover|ebook|audiobook) "paperback"

>


<!ELEMENT title (#PCDATA)>

<!ELEMENT author (first-name, last-name)>

<!ELEMENT first-name (#PCDATA)>

<!ELEMENT last-name (#PCDATA)>

<!ELEMENT year (#PCDATA)>

<!ELEMENT price (#PCDATA)>

<!ATTLIST price

    currency (USD|EUR|GBP|JPY|CAD) "USD"

>


<!ELEMENT isbn (#PCDATA)>

<!ELEMENT publisher (#PCDATA)>

<!ELEMENT stock (quantity, location)>

<!ELEMENT quantity (#PCDATA)>

<!ELEMENT location (#PCDATA)>

<!ELEMENT description (#PCDATA)>





3. XSD Validation (bookstore.xsd)




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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">


    <!-- Root element -->

    <xs:element name="bookstore">

        <xs:complexType>

            <xs:sequence>

                <xs:element name="book" type="bookType" maxOccurs="unbounded"/>

            </xs:sequence>

        </xs:complexType>

    </xs:element>


    <!-- Book Type -->

    <xs:complexType name="bookType">

        <xs:sequence>

            <xs:element name="title" type="xs:string"/>

            <xs:element name="author" type="authorType"/>

            <xs:element name="year" type="yearType"/>

            <xs:element name="price" type="priceType"/>

            <xs:element name="isbn" type="isbnType"/>

            <xs:element name="publisher" type="xs:string"/>

            <xs:element name="stock" type="stockType"/>

            <xs:element name="description" type="xs:string"/>

        </xs:sequence>

        <xs:attribute name="id" type="xs:ID" use="required"/>

        <xs:attribute name="category" use="required">

            <xs:simpleType>

                <xs:restriction base="xs:string">

                    <xs:enumeration value="fiction"/>

                    <xs:enumeration value="non-fiction"/>

                    <xs:enumeration value="children"/>

                    <xs:enumeration value="science"/>

                    <xs:enumeration value="biography"/>

                    <xs:enumeration value="history"/>

                </xs:restriction>

            </xs:simpleType>

        </xs:attribute>

        <xs:attribute name="format" default="paperback">

            <xs:simpleType>

                <xs:restriction base="xs:string">

                    <xs:enumeration value="paperback"/>

                    <xs:enumeration value="hardcover"/>

                    <xs:enumeration value="ebook"/>

                    <xs:enumeration value="audiobook"/>

                </xs:restriction>

            </xs:simpleType>

        </xs:attribute>

    </xs:complexType>


    <!-- Author Type -->

    <xs:complexType name="authorType">

        <xs:sequence>

            <xs:element name="first-name" type="xs:string"/>

            <xs:element name="last-name" type="xs:string"/>

        </xs:sequence>

    </xs:complexType>


    <!-- Year Type with constraint -->

    <xs:simpleType name="yearType">

        <xs:restriction base="xs:integer">

            <xs:minInclusive value="1000"/>

            <xs:maxInclusive value="2024"/>

        </xs:restriction>

    </xs:simpleType>


    <!-- Price Type -->

    <xs:complexType name="priceType">

        <xs:simpleContent>

            <xs:extension base="xs:decimal">

                <xs:attribute name="currency" default="USD">

                    <xs:simpleType>

                        <xs:restriction base="xs:string">

                            <xs:enumeration value="USD"/>

                            <xs:enumeration value="EUR"/>

                            <xs:enumeration value="GBP"/>

                            <xs:enumeration value="JPY"/>

                            <xs:enumeration value="CAD"/>

                        </xs:restriction>

                    </xs:simpleType>

                </xs:attribute>

            </xs:extension>

        </xs:simpleContent>

    </xs:complexType>


    <!-- ISBN Type with pattern -->

    <xs:simpleType name="isbnType">

        <xs:restriction base="xs:string">

            <xs:pattern value="97[89]-\d{1,5}-\d{1,7}-\d{1,7}-\d{1}"/>

            <xs:pattern value="\d{10}"/>

            <xs:pattern value="\d{13}"/>

        </xs:restriction>

    </xs:simpleType>


    <!-- Stock Type -->

    <xs:complexType name="stockType">

        <xs:sequence>

            <xs:element name="quantity" type="xs:nonNegativeInteger"/>

            <xs:element name="location" type="xs:string"/>

        </xs:sequence>

    </xs:complexType>


</xs:schema>



4. Validation Commands

Using xmllint (Linux/Mac):





# Validate with DTD

xmllint --valid --noout bookstore.xml


# Validate with XSD

xmllint --schema bookstore.xsd --noout bookstore.xml





Using Python:



# validate_dtd.py

import xml.dom.minidom

import xml.sax


# DTD Validation

try:

    parser = xml.sax.make_parser()

    parser.setFeature(xml.sax.handler.feature_validation, True)

    parser.parse("bookstore.xml")

    print("DTD validation: PASSED")

except Exception as e:

    print(f"DTD validation: FAILED - {e}")


# XSD Validation

from lxml import etree


try:

    xml_doc = etree.parse("bookstore.xml")

    xsd_doc = etree.parse("bookstore.xsd")

    xsd = etree.XMLSchema(xsd_doc)

    

    if xsd.validate(xml_doc):

        print("XSD validation: PASSED")

    else:

        print("XSD validation: FAILED")

        print(xsd.error_log)

except Exception as e:

    print(f"XSD validation error: {e}") 



Output:



Comments