Created
July 10, 2024 11:25
-
-
Save birkin/e7a6e1b070bb793a0fd643de65f712f1 to your computer and use it in GitHub Desktop.
code to validate xml agains XSD schema with no network access.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from lxml import etree | |
def validate_xml_with_schema( xml_filepath: str, xsd_filepath: str ) -> None: | |
""" | |
Validates an XML file against an XSD schema, without network access. | |
Confirms that: | |
- xmlcatalog is routing the schema location to the local file system | |
- the C `libxml2` library used by lxml does auto-default to the standard server's `xml/catalog` file. | |
- so this should not be necessary: ```os.environ['XML_CATALOG_FILES'] = '/path/to/xml/catalog'``` | |
""" | |
schema_obj = etree.XMLSchema( etree.XML(open(xsd_filepath, 'rb').read()) ) | |
parser_obj = etree.XMLParser( schema=schema_obj, no_network=True ) | |
try: | |
etree.parse( xml_filepath, parser_obj ) # since the parser is buil with the schema, it will auto-validate the XML, no need for `schema.assertValid( doc )` | |
print( "XML is valid according to the schema." ) | |
except etree.DocumentInvalid as e: | |
print( f"XML validation error: {e}" ) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment