Create an xml for the bookstore.
Validate the same using both DTD and XSD
[Link]:
<?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>
[Link]:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="[Link]
<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>
[Link]:
<!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)>
[Link]:
from lxml import etree
def validate_with_dtd(xml_file, dtd_file):
# Parse DTD
dtd = [Link](open(dtd_file))
# Parse XML
xml = [Link](xml_file)
# Validate XML against DTD
if [Link](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 = "[Link]"
dtd_file = "[Link]"
# Validate XML against DTD
validate_with_dtd(xml_file, dtd_file)