Skip to main content

An XML Schema validator and decoder

Project description

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

This is a library that 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.

Features

The xmlschema library includes the following features:

  • Builds XML schema objects from XSD files

  • Validates the XML instances with the XSD schema

  • Converts XML instances into Python nested dictionaries

  • Provides decoding and encoding APIs for XML’s elements and attributes

Installation

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.

Usage

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

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

The schema can be used to validate XML documents:

>>> my_schema.is_valid('xmlschema/tests/examples/vehicles/vehicles.xml')
True
>>> my_schema.is_valid('xmlschema/tests/examples/vehicles/vehicles-1_error.xml')
False
>>> my_schema.validate('xmlschema/tests/examples/vehicles/vehicles-1_error.xml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/brunato/Development/projects/xmlschema/xmlschema/schema.py", line 220, in validate
    raise error
xmlschema.exceptions.XMLSchemaValidationError: failed validating <Element ...

Reason: character data between child elements not allowed!

Schema:

  <xs:sequence xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element maxOccurs="unbounded" minOccurs="0" name="car" type="vh:vehicleType" />
  </xs:sequence>

Instance:

  <ns0:cars xmlns:ns0="http://example.com/vehicles">
    NOT ALLOWED CHARACTER DATA
    <ns0:car make="Porsche" model="911" />
    <ns0:car make="Porsche" model="911" />
  </ns0:cars>

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

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

License

This software is distributed under the terms of the MIT License. See the file ‘LICENSE’ in the root directory of the present distribution, or http://opensource.org/licenses/MIT.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmlschema-0.9.9.tar.gz (108.8 kB view details)

Uploaded Source

Built Distribution

xmlschema-0.9.9-py2.py3-none-any.whl (142.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file xmlschema-0.9.9.tar.gz.

File metadata

  • Download URL: xmlschema-0.9.9.tar.gz
  • Upload date:
  • Size: 108.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xmlschema-0.9.9.tar.gz
Algorithm Hash digest
SHA256 7f15f0c17b364636bf44508e7bba08580adaefcf1b0ceb34b3ce08aee49ef369
MD5 b51aa1914a09776733feff65bfbdb72d
BLAKE2b-256 660b27c419c0d2c31d9b4fd190e5745de6938a075764194bbd08a448ba14acd4

See more details on using hashes here.

File details

Details for the file xmlschema-0.9.9-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for xmlschema-0.9.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f3be66aa033ecbf6ff767c4e778e01c27ef8a085b8a0e364fb902be6e5af1b5d
MD5 80883e01dbf64f8dd6108cef1cab1aea
BLAKE2b-256 80252e499e3ae9efb31e66b8a474f75c828ef94821f0ff5373d918baf969a6f8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page