Skip to main content

An XML Schema validator and decoder

Project description

https://img.shields.io/pypi/v/xmlschema.svg https://img.shields.io/pypi/pyversions/xmlschema.svg https://img.shields.io/pypi/implementation/xmlschema.svg MIT License https://travis-ci.org/sissaschool/xmlschema.svg?branch=master https://img.shields.io/pypi/dm/xmlschema.svg https://img.shields.io/badge/Maintained%3F-yes-green.svg

The xmlschema library is an implementation of XML Schema for Python (supports Python 3.6+).

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.

The full xmlschema documentation is available on “Read the Docs”.

Features

This library includes the following features:

  • Full XSD 1.0 and XSD 1.1 support

  • Building of XML schema objects from XSD files

  • Validation of XML instances against XSD schemas

  • Decoding of XML data into Python data and to JSON

  • Encoding of Python data and JSON to XML

  • Data decoding and encoding ruled by converter classes

  • An XPath based API for finding schema’s elements and attributes

  • Support of XSD validation modes strict/lax/skip

  • Remote attacks protection by default using an XMLParser that forbids entities

  • XML data bindings based on DataElement class

  • Static code generation with Jinja2 templates

Installation

You can install the library with pip in a Python 3.6+ environment:

pip install xmlschema

The library uses the Python’s ElementTree XML library and requires elementpath additional package. The base schemas of the XSD standards are included in the package for working offline and to speed-up the building of schema instances.

Usage

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('tests/test_cases/examples/vehicles/vehicles.xsd')

The schema can be used to validate XML documents:

>>> my_schema.is_valid('tests/test_cases/examples/vehicles/vehicles.xml')
True
>>> my_schema.is_valid('tests/test_cases/examples/vehicles/vehicles-1_error.xml')
False
>>> my_schema.validate('tests/test_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/xsdbase.py", line 393, in validate
    raise error
xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element '{http://example.com/vehicles}cars' at 0x7f8032768458> with XsdGroup(model='sequence').

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:

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

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('tests/test_cases/examples/collection/collection.xsd')
>>> pprint(xs.to_dict('tests/test_cases/examples/collection/collection.xml'))
{'@xsi:schemaLocation': 'http://example.com/ns/collection 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'}]}

Authors

Davide Brunato and others who have contributed with code or with sample cases.

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

This version

1.9.1

Download files

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

Source Distribution

xmlschema-1.9.1.tar.gz (394.7 kB view details)

Uploaded Source

Built Distribution

xmlschema-1.9.1-py3-none-any.whl (273.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xmlschema-1.9.1.tar.gz
  • Upload date:
  • Size: 394.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.0

File hashes

Hashes for xmlschema-1.9.1.tar.gz
Algorithm Hash digest
SHA256 91ebcb6b92f7db1243904b4f6d11f8b354a059d4d9fa4880b52f132a24a3226f
MD5 2c6ded88efc38e5915c407d25fb936a4
BLAKE2b-256 fc89cafdaabbf2725472f0b7feaa6daf1883b65742dd5137f03a2f32da852947

See more details on using hashes here.

File details

Details for the file xmlschema-1.9.1-py3-none-any.whl.

File metadata

  • Download URL: xmlschema-1.9.1-py3-none-any.whl
  • Upload date:
  • Size: 273.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.10.0

File hashes

Hashes for xmlschema-1.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99b24c60efaa2cc3a1fe52f40a4f7a213e317a0b0ae255550bb4f0f91ce4ca72
MD5 7144eea40e6084355b78ead7d27af540
BLAKE2b-256 4eb988fe96ef524835017f3f1965e93941421bdc61bba8009fa19dbe80066a31

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