[Python] – XML Validator against XML Schema (XSD)
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The World Wide Web Consortium’s XML 1.0 Specification of 1998 and several other related specifications—all of them free open standards—define XML.
XSD (XML Schema Definition) is a World Wide Web Consortium (W3C) recommendation that specifies how to formally describe the elements in an Extensible Markup Language (XML) document. This description can be used to verify that each item of content in a document adheres to the description of the element in which the content is to be placed. XSD 1.1 became an approved W3C standard in April 2012.
This code below receives an xml file and an xsd file as input and responds if the xml is xsd compliant. It is very useful in many real-world scenarios.
from easyxsd import * import sys # Load XML Schema (.xsd file) xsd = xsd_from_file(sys.argv[1]) # Load XML File xml = xml_from_file(sys.argv[2]) # Validate list_errors = validate_with_errors(xml, xsd) # Open output file (validation response) f = open(sys.argv[3],'w') for error in list_errors: f.write(str(error) + '\n') f.close()
Download here an executable version for Windows
To execute:
validate_xml.exe C:\Documents\MyFolder\schema.xsd C:\Documents\MyFolder\myfile.xml C:\Documents\MyFolder\outputvalidate.txt
The shortcut works too:
validate_xml.exe schema.xsd myfile.xml outputvalidate.txt