0% found this document useful (0 votes)
22 views3 pages

Program 6

Uploaded by

217r1a0597
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Program 6

Uploaded by

217r1a0597
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Create an xml for the bookstore.

Validate the same using both DTD and XSD


Bookstore.xml:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title>Learning Python</title>
<author>Mark Lutz</author>
<price>39.95</price>
<publish_date>2013-06-01</publish_date>
<description>An introduction to Python programming.</description>
</book>
<book>
<title>Effective Java</title>
<author>Joshua Bloch</author>
<price>45.00</price>
<publish_date>2018-01-01</publish_date>
<description>A comprehensive guide to Java programming.</description>
</book>
</bookstore>
Bookstore.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="publish_date" type="xs:date"/>
<xs:element name="description" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Bookstore.dtd:
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, author, price, publish_date, description)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT publish_date (#PCDATA)>
<!ELEMENT description (#PCDATA)>
Validate.py:
from lxml import etree
def validate_with_dtd(xml_file, dtd_file):
# Parse DTD
dtd = etree.DTD(open(dtd_file))
# Parse XML
xml = etree.parse(xml_file)
# Validate XML against DTD
if dtd.validate(xml):
print("XML is valid against DTD")
else:
print("XML is NOT valid against DTD")
print(dtd.error_log)
# Path to XML and DTD files
xml_file = "Bookstore.xml"
dtd_file = "Bookstore.dtd"
# Validate XML against DTD
validate_with_dtd(xml_file, dtd_file)

You might also like