xmlschema 4.0.1


pip install xmlschema

  Latest version

Released: Apr 13, 2025

Project Links

Meta
Author: Davide Brunato
Requires Python: >=3.9

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers
  • Information Technology
  • Science/Research

Operating System
  • OS Independent

Programming Language
  • Python
  • Python :: 3
  • Python :: 3 :: Only
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: 3.14
  • Python :: Implementation :: CPython
  • Python :: Implementation :: PyPy

Topic
  • Software Development :: Libraries
  • Text Processing :: Markup :: XML
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://img.shields.io/pypi/dm/xmlschema.svg

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

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

  • XML attacks protection using an XMLParser that forbids entities

  • Access control on resources addressed by an URL or filesystem path

  • Downloading XSD files from a remote URL and storing them for offline use

  • 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.7+ 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.

4.0.1 Apr 13, 2025
4.0.0 Apr 11, 2025
3.4.5 Mar 22, 2025
3.4.4 Mar 18, 2025
3.4.3 Oct 31, 2024
3.4.2 Sep 18, 2024
3.4.1 Sep 12, 2024
3.4.0 Sep 10, 2024
3.3.2 Jul 29, 2024
3.3.1 Apr 28, 2024
3.3.0 Apr 17, 2024
3.2.1 Apr 07, 2024
3.2.0 Mar 25, 2024
3.1.0 Mar 13, 2024
3.0.2 Feb 18, 2024
3.0.1 Jan 09, 2024
3.0.0 Jan 07, 2024
2.5.1 Dec 19, 2023
2.5.0 Sep 21, 2023
2.4.0 Jul 27, 2023
2.3.1 Jun 14, 2023
2.3.0 May 18, 2023
2.2.3 Apr 14, 2023
2.2.2 Mar 05, 2023
2.2.1 Feb 11, 2023
2.2.0 Feb 06, 2023
2.1.1 Oct 01, 2022
2.1.0 Sep 25, 2022
2.0.4 Sep 08, 2022
2.0.3 Aug 25, 2022
2.0.2 Aug 12, 2022
2.0.1 Jul 21, 2022
2.0.0 Jul 18, 2022
1.11.3 Jun 24, 2022
1.11.2 Jun 11, 2022
1.11.1 May 22, 2022
1.11.0 May 14, 2022
1.10.0 Mar 07, 2022
1.9.2 Dec 23, 2021
1.9.1 Dec 08, 2021
1.9.0 Nov 30, 2021
1.8.2 Nov 11, 2021
1.8.1 Oct 20, 2021
1.8.0 Sep 27, 2021
1.7.1 Sep 03, 2021
1.7.0 Aug 02, 2021
1.6.4 Jun 09, 2021
1.6.3 Jun 07, 2021
1.6.2 May 03, 2021
1.6.1 Apr 11, 2021
1.6.0 Apr 06, 2021
1.5.3 Mar 14, 2021
1.5.2 Mar 04, 2021
1.5.1 Feb 11, 2021
1.5.0 Feb 05, 2021
1.4.2 Jan 25, 2021
1.4.1 Dec 24, 2020
1.4.0 Dec 23, 2020
1.3.1 Nov 10, 2020
1.3.0 Nov 09, 2020
1.2.5 Sep 26, 2020
1.2.4 Sep 13, 2020
1.2.3 Aug 14, 2020
1.2.2 Jun 16, 2020
1.2.1 Jun 12, 2020
1.2.0 May 28, 2020
1.1.3 Apr 28, 2020
1.1.2 Mar 22, 2020
1.1.1 Feb 19, 2020
1.1.0 Jan 23, 2020
1.0.18 Dec 24, 2019
1.0.17 Dec 21, 2019
1.0.16 Nov 18, 2019
1.0.15 Oct 13, 2019
1.0.14 Aug 27, 2019
1.0.13 Jun 19, 2019
1.0.11 May 05, 2019
1.0.10 Feb 25, 2019
1.0.9 Feb 03, 2019
1.0.8 Jan 30, 2019
1.0.7 Nov 15, 2018
1.0.6 Oct 21, 2018
1.0.5 Sep 27, 2018
1.0.4 Sep 22, 2018
1.0.3 Aug 27, 2018
1.0.2 Jul 26, 2018
1.0.1 Jul 14, 2018
0.9.31 Jun 24, 2018
0.9.30 Jun 07, 2018
0.9.29 Jun 03, 2018
0.9.28 May 18, 2018
0.9.27 May 08, 2018
0.9.26 Apr 12, 2018
0.9.25 Apr 05, 2018
0.9.24 Apr 03, 2018
0.9.23 Mar 10, 2018
0.9.22 Feb 16, 2018
0.9.20 Jan 22, 2018
0.9.18 Jan 12, 2018
0.9.17 Dec 28, 2017
0.9.16 Dec 23, 2017
0.9.15 Dec 15, 2017
0.9.14 Nov 23, 2017
0.9.13 Oct 12, 2017
0.9.12 Sep 14, 2017
0.9.11 Jul 12, 2017
0.9.10 Jul 08, 2017
0.9.9 Jun 12, 2017
0.9.8 May 27, 2017
0.9.7 May 22, 2017

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies:
elementpath (<6.0.0,>=4.8.0)