Search code examples
pythonxmlpyxb

PYXB: Ignore missing attribute


I am parsing some XML using Pyxb and a required attribute is not available in the XML. I catch the MissingAttributeError exception which tells me:

Instance of <class 'TestXML.TEST.CTD_ANON_27'> lacks required attribute how

My question is, even though a required attribute is missing from the XML data, is there a way to still parse this XML data even though this attribute is missing? The rest of the XML data is valid and still useful.


Solution

  • You can disable validation. Something like the following (excerpt from the test suite):

    xmls = '<Element/>'
    pyxb.RequireValidWhenParsing(True)
    self.assertRaises(MissingAttributeError, CreateFromDocument, xmls)
    pyxb.RequireValidWhenParsing(False)
    self.assertFalse(pyxb._ParsingRequiresValid)
    instance = CreateFromDocument(xmls)
    

    Note that PyXB is not intended to support processing invalid documents, so the generated binding may not fully reflect the content of the XML.