0
votes

I am trying to validate a simple XML document against the B2MML schema (multiple XSD documents) using lxml. lxml returns "No matching global declaration available for the validation root." This type of question has been posted before, but none of the solutions appear to work.

XML document requiring validation:

<?xml version="1.0" encoding="UTF-8"?>
<MaterialLot xmlns="http://www.w3.org/2001/XMLSchema">
    <ID>000010002377</ID>
</MaterialLot>

The code used to validate the document:

def validate_xml(xmlschemafile, xmlfilename):

    with open(xmlschemafile, 'r') as f:
        schema_root = etree.XML(f.read())

    schema = etree.XMLSchema(schema_root)
    xmlparser = etree.XMLParser(schema=schema)

    try:
        with open(xmlfilename, 'r') as f:
            etree.fromstring(f.read(), xmlparser)
        return True
if __name__ == '__main__':
    validate_xml(xmlschema, filename)

And the B2MML schema files are found here: https://github.com/werdnav/b2mml-validation

The XSD name space is 'http://www.w3.org/2001/XMLSchema' (read from the XSD documents) which exactly matches the XML example. However, the validator code (above) returns:

Element '{http://www.w3.org/2001/XMLSchema}MaterialLot': No matching global declaration available for the validation root.

Which is very odd, given MaterialLot is declared globally.

1

1 Answers

0
votes

Turns out the namespace was incorrect. The correct namespace for B2MML is 'http://www.wbf.org/xml/B2MML-V0401'. Therefore, the XML example (above) should read:

<?xml version="1.0" encoding="UTF-8"?>
<MaterialLot xmlns="http://www.wbf.org/xml/B2MML-V0401">
    <ID>000010002377</ID>
</MaterialLot>