Home > Software engineering >  py xmlschema: element * at * is not an element of the schema
py xmlschema: element * at * is not an element of the schema

Time:10-22

My simplified xsd/xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
  <xs:element name="modal" />
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<modal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../src/customElements/modal.xsd" />

I'm using xmlschema in python, getting the following output/error:

>>> import xmlschema
>>> schema = xmlschema.XMLSchema('src/customElements/modal.xsd')
>>> schema.is_valid('dist/modal.xml')
False
>>> schema.validate('dist/modal.xml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\ProgramFilesFolder\Python\lib\site-packages\xmlschema\validators\schemas.py", line 1704, in validate
    raise error
xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element 'modal' at 0x00000264CD9D33D0> with XMLSchema10(name='modal.xsd', namespace=''):

Reason: <Element 'modal' at 0x00000264CD9D33D0> is not an element of the schema

Instance:

  <modal xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../src/customElements/modal.xsd" />

Path: /modal
>>> 

inside app folder, app directory looks like this:

app
│   
├───dist
│       modal.xml
│           
├───src
│   ├───customElements
│   │       modal.xsd

CodePudding user response:

The error is not relative to the filepaths but to the vc:minVersion="1.1" present in your xmlschema that succeeds if the XSD version supported by the processor is greater than or equal to your vc:minVersion, in this case 1.1. In your case it fails because the default xmlschema guarantees just the XSD 1.0 support: to solve your problem you have to replace schema = xmlschema.XMLSchema('src/customElements/modal.xsd') explicitly with the new line schema = xmlschema.XMLSchema11('src/customElements/modal.xsd') leaving the other lines of your program unaltered.

  • Related