Using an XML catalog with Python's lxml?

lxml, python, xml

Solution

You can add the catalog to the `XML_CATALOG_FILES` environment variable:

os.environ['XML_CATALOG_FILES'] = 'file:///to/my/catalog.xml'

See this thread. Note that entries in `XML_CATALOG_FILES` are space-separated URLs. You can use Python's `pathname2url` and `urljoin` (with `file:`) to generate the URL from a pathname.

Problem

Is there a way, when I parse an XML document using lxml, to validate that document against its DTD using an external catalog file? I need to be able to work the fixed attributes defined in a document’s DTD.

Original source