XML validation ensures that an XML document conforms to a predefined structure using XSD schemas. This tutorial demonstrates how to validate XML files in Python with lxml.
<!-- Define XML Data -->
<students>
<student>
<id>1</id>
<name>John Deo</name>
<mark>75</mark>
</student>
</students>
<!-- Define XSD Schema -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="students">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:integer"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="mark" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
from lxml import etree
# Define XML data
xml_data = """
<students>
<student>
<id>1</id>
<name>John Deo</name>
<mark>75</mark>
</student>
</students>
"""
# Define XSD schema
xsd_data = """
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="students">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="id" type="xsd:integer"/>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="mark" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
"""
# Parse XML and XSD
xml_doc = etree.fromstring(xml_data)
xsd_doc = etree.XMLSchema(etree.fromstring(xsd_data))
# Validate XML
if xsd_doc.validate(xml_doc):
print("XML is valid.")
else:
print("XML is invalid.")
print(xsd_doc.error_log)
XML is valid.
Validating XML ensures its structure and data integrity. Python's lxml library offers a robust solution for this, enabling error-free XML processing.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.