Skip to main content

xmlschema

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.

Features

The xmlschema library includes the following features:

  • Building of XML schema objects from XSD files

  • Validation of XML instances with 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

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 a schema instance 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 match 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.

Roadmap

  • Full XSD 1.0 support (almost done)

  • Validated XML data encoding

  • XSD 1.1

Metadata

Release files for xmlschema 0.9.16

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xmlschema 0.9.16
File Size Uploaded Provenance Actions
xmlschema-0.9.16.tar.gz 127.5 kB Without provenance Details for xmlschema-0.9.16.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xmlschema 0.9.16
File Interpreter ABI Platform Actions
xmlschema-0.9.16-py2.py3-none-any.whl Python 2, Python 3 none any Details for xmlschema-0.9.16-py2.py3-none-any.whl

Total release size: 392.3 kB

Release files / xmlschema-0.9.16.tar.gz

Download URL xmlschema-0.9.16.tar.gz
Size 127.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c2db28782545232568df7c09070c5ebd4f77c7e5026cfe04f653e53cac807554
BLAKE2b-256 checksum
How to use checksums
b98f450aee7049696d1207b46a517274a4ca00c505cb0ada8c0a7f79ae981a10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / xmlschema-0.9.16-py2.py3-none-any.whl

Download URL xmlschema-0.9.16-py2.py3-none-any.whl
Size 264.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
633fa5df12bd5d9b2892b3d20035fe129cdc596ee2e3a0a8570b4fbd48459a2f
BLAKE2b-256 checksum
How to use checksums
4ca92bb6e990bd58abb204f90f00fc884d188bc10adf4bbf00fb9e5f84240571
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for xmlschema 0.9.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.16 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page