skip to navigation
skip to content

xmlschema 0.9.23

An XML Schema validator and decoder

This is an implementation of XML Schema for Python (supports versions 2.7 and Python 3.3+).

This library arises from the needs of a solid Python layer for processing XML Schema based files for MaX (Materials design at the Exascale) European project. A significant problem is the encoding and the decoding of the XML data files produced by different simulation software. Another important requirement is the XML data validation, in order to put the produced data under control. The lack of a suitable alternative for Python in the schema-based decoding of XML data has led to build this library. Obviously this library can be useful for other cases related to XML Schema based processing, not only for the original scope.

For more information read the xmlschema documentation on readthedocs.


The xmlschema library includes the following features:

  • Full XSD 1.0 support
  • Building of XML schema objects from XSD files
  • Validation of XML instances against XSD schemas
  • Decoding of XML data into Python data structures
  • An XPath based API for finding schema’s elements and attributes
  • Support of XSD validation modes


You can install the library with pip in a Python 2.7 or Python 3.3+ environment:

pip install xmlschema

The library uses the Python’s ElementTree XML library and doesn’t require additional packages. The library includes also the schemas of the XML Schema standards for working offline and to speed-up the building of schema instances.


Import the library and then create a schema instance using the path of the file containing the schema as argument:

>>> import xmlschema
>>> my_schema = xmlschema.XMLSchema('xmlschema/tests/cases/examples/vehicles/vehicles.xsd')

The schema can be used to validate XML documents:

>>> my_schema.is_valid('xmlschema/tests/cases/examples/vehicles/vehicles.xml')
>>> my_schema.is_valid('xmlschema/tests/cases/examples/vehicles/vehicles-1_error.xml')
>>> my_schema.validate('xmlschema/tests/cases/examples/vehicles/vehicles-1_error.xml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/brunato/Development/projects/xmlschema/xmlschema/validators/", line 393, in validate
    raise error
xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element '{}cars' at 0x7f8032768458> with XsdGroup(model='sequence').

Reason: character data between child elements not allowed!


  <xs:sequence xmlns:xs="">
        <xs:element maxOccurs="unbounded" minOccurs="0" name="car" type="vh:vehicleType" />


  <vh:cars xmlns:vh="">
    <vh:car make="Porsche" model="911" />
    <vh:car make="Porsche" model="911" />

Using a schema you can also decode the XML documents to nested dictionaries, with values that match to the data types declared by the schema:

>>> import xmlschema
>>> from pprint import pprint
>>> xs = xmlschema.XMLSchema('xmlschema/tests/cases/examples/collection/collection.xsd')
>>> pprint(xs.to_dict('xmlschema/tests/cases/examples/collection/collection.xml'))
{'@xsi:schemaLocation': ' collection.xsd',
 'object': [{'@available': True,
             '@id': 'b0836217462',
             'author': {'@id': 'PAR',
                        'born': '1841-02-25',
                        'dead': '1919-12-03',
                        'name': 'Pierre-Auguste Renoir',
                        'qualification': 'painter'},
             'estimation': Decimal('10000.00'),
             'position': 1,
             'title': 'The Umbrellas',
             'year': '1886'},
            {'@available': True,
             '@id': 'b0836217463',
             'author': {'@id': 'JM',
                        'born': '1893-04-20',
                        'dead': '1983-12-25',
                        'name': 'Joan Miró',
                        'qualification': 'painter, sculptor and ceramicist'},
             'position': 2,
             'title': None,
             'year': '1925'}]}


This software is distributed under the terms of the MIT License. See the file ‘LICENSE’ in the root directory of the present distribution, or


  • Validated XML data encoding
  • XSD 1.1
File Type Py Version Uploaded on Size
xmlschema-0.9.23-py2.py3-none-any.whl (md5) Python Wheel py2.py3 2018-03-10 191KB
xmlschema-0.9.23.tar.gz (md5) Source 2018-03-10 135KB