xmlschema


Namexmlschema JSON
Version 3.4.3 PyPI version JSON
download
home_pagehttps://github.com/sissaschool/xmlschema
SummaryAn XML Schema validator and decoder
upload_time2024-10-31 09:47:17
maintainerNone
docs_urlNone
authorDavide Brunato
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            *********
xmlschema
*********

.. image:: https://img.shields.io/pypi/v/xmlschema.svg
   :target: https://pypi.python.org/pypi/xmlschema/
.. image:: https://img.shields.io/pypi/pyversions/xmlschema.svg
   :target: https://pypi.python.org/pypi/xmlschema/
.. image:: https://img.shields.io/pypi/implementation/xmlschema.svg
   :target: https://pypi.python.org/pypi/xmlschema/
.. image:: https://img.shields.io/badge/License-MIT-blue.svg
   :alt: MIT License
   :target: https://lbesson.mit-license.org/
.. image:: https://img.shields.io/pypi/dm/xmlschema.svg
   :target: https://pypi.python.org/pypi/xmlschema/

.. xmlschema-introduction-start

The *xmlschema* library is an implementation of `XML Schema <http://www.w3.org/2001/XMLSchema>`_
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) <http://www.max-centre.eu>`_  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" <http://xmlschema.readthedocs.io/en/latest/>`_.


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 <https://github.com/brunato/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.

.. xmlschema-introduction-end


Usage
=====

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

.. code-block:: pycon

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

.. note::
    For XSD 1.1 schemas use the class `XMLSchema11`, because the default class
    `XMLSchema` is an alias of the XSD 1.0 validator class `XMLSchema10`.

The schema can be used to validate XML documents:

.. code-block:: pycon

    >>> 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:

.. code-block:: pycon

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sissaschool/xmlschema",
    "name": "xmlschema",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Davide Brunato",
    "author_email": "brunato@sissa.it",
    "download_url": "https://files.pythonhosted.org/packages/11/ca/56579e6b4558c3c06902fe6647280345da8087f080c86e85166fd2131f8d/xmlschema-3.4.3.tar.gz",
    "platform": null,
    "description": "*********\nxmlschema\n*********\n\n.. image:: https://img.shields.io/pypi/v/xmlschema.svg\n   :target: https://pypi.python.org/pypi/xmlschema/\n.. image:: https://img.shields.io/pypi/pyversions/xmlschema.svg\n   :target: https://pypi.python.org/pypi/xmlschema/\n.. image:: https://img.shields.io/pypi/implementation/xmlschema.svg\n   :target: https://pypi.python.org/pypi/xmlschema/\n.. image:: https://img.shields.io/badge/License-MIT-blue.svg\n   :alt: MIT License\n   :target: https://lbesson.mit-license.org/\n.. image:: https://img.shields.io/pypi/dm/xmlschema.svg\n   :target: https://pypi.python.org/pypi/xmlschema/\n\n.. xmlschema-introduction-start\n\nThe *xmlschema* library is an implementation of `XML Schema <http://www.w3.org/2001/XMLSchema>`_\nfor Python (supports Python 3.8+).\n\nThis library arises from the needs of a solid Python layer for processing XML\nSchema based files for\n`MaX (Materials design at the Exascale) <http://www.max-centre.eu>`_  European project.\nA significant problem is the encoding and the decoding of the XML data files\nproduced by different simulation software.\nAnother important requirement is the XML data validation, in order to put the\nproduced data under control. The lack of a suitable alternative for Python in\nthe schema-based decoding of XML data has led to build this library. Obviously\nthis library can be useful for other cases related to XML Schema based processing,\nnot only for the original scope.\n\nThe full `xmlschema documentation is available on \"Read the Docs\" <http://xmlschema.readthedocs.io/en/latest/>`_.\n\n\nFeatures\n========\n\nThis library includes the following features:\n\n* Full XSD 1.0 and XSD 1.1 support\n* Building of XML schema objects from XSD files\n* Validation of XML instances against XSD schemas\n* Decoding of XML data into Python data and to JSON\n* Encoding of Python data and JSON to XML\n* Data decoding and encoding ruled by converter classes\n* An XPath based API for finding schema's elements and attributes\n* Support of XSD validation modes *strict*/*lax*/*skip*\n* XML attacks protection using an XMLParser that forbids entities\n* Access control on resources addressed by an URL or filesystem path\n* Downloading XSD files from a remote URL and storing them for offline use\n* XML data bindings based on DataElement class\n* Static code generation with Jinja2 templates\n\n\nInstallation\n============\n\nYou can install the library with *pip* in a Python 3.7+ environment::\n\n    pip install xmlschema\n\nThe library uses the Python's ElementTree XML library and requires\n`elementpath <https://github.com/brunato/elementpath>`_ additional package.\nThe base schemas of the XSD standards are included in the package for working\noffline and to speed-up the building of schema instances.\n\n.. xmlschema-introduction-end\n\n\nUsage\n=====\n\nImport the library and then create a schema instance using the path of\nthe file containing the schema as argument:\n\n.. code-block:: pycon\n\n    >>> import xmlschema\n    >>> my_schema = xmlschema.XMLSchema('tests/test_cases/examples/vehicles/vehicles.xsd')\n\n.. note::\n    For XSD 1.1 schemas use the class `XMLSchema11`, because the default class\n    `XMLSchema` is an alias of the XSD 1.0 validator class `XMLSchema10`.\n\nThe schema can be used to validate XML documents:\n\n.. code-block:: pycon\n\n    >>> my_schema.is_valid('tests/test_cases/examples/vehicles/vehicles.xml')\n    True\n    >>> my_schema.is_valid('tests/test_cases/examples/vehicles/vehicles-1_error.xml')\n    False\n    >>> my_schema.validate('tests/test_cases/examples/vehicles/vehicles-1_error.xml')\n    Traceback (most recent call last):\n      File \"<stdin>\", line 1, in <module>\n      File \"/home/brunato/Development/projects/xmlschema/xmlschema/validators/xsdbase.py\", line 393, in validate\n        raise error\n    xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element '{http://example.com/vehicles}cars' at 0x7f8032768458> with XsdGroup(model='sequence').\n\n    Reason: character data between child elements not allowed!\n\n    Schema:\n\n      <xs:sequence xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n            <xs:element maxOccurs=\"unbounded\" minOccurs=\"0\" name=\"car\" type=\"vh:vehicleType\" />\n      </xs:sequence>\n\n    Instance:\n\n      <vh:cars xmlns:vh=\"http://example.com/vehicles\">\n        NOT ALLOWED CHARACTER DATA\n        <vh:car make=\"Porsche\" model=\"911\" />\n        <vh:car make=\"Porsche\" model=\"911\" />\n      </vh:cars>\n\nUsing a schema you can also decode the XML documents to nested dictionaries, with\nvalues that match to the data types declared by the schema:\n\n.. code-block:: pycon\n\n    >>> import xmlschema\n    >>> from pprint import pprint\n    >>> xs = xmlschema.XMLSchema('tests/test_cases/examples/collection/collection.xsd')\n    >>> pprint(xs.to_dict('tests/test_cases/examples/collection/collection.xml'))\n    {'@xsi:schemaLocation': 'http://example.com/ns/collection collection.xsd',\n     'object': [{'@available': True,\n                 '@id': 'b0836217462',\n                 'author': {'@id': 'PAR',\n                            'born': '1841-02-25',\n                            'dead': '1919-12-03',\n                            'name': 'Pierre-Auguste Renoir',\n                            'qualification': 'painter'},\n                 'estimation': Decimal('10000.00'),\n                 'position': 1,\n                 'title': 'The Umbrellas',\n                 'year': '1886'},\n                {'@available': True,\n                 '@id': 'b0836217463',\n                 'author': {'@id': 'JM',\n                            'born': '1893-04-20',\n                            'dead': '1983-12-25',\n                            'name': 'Joan Mir\u00f3',\n                            'qualification': 'painter, sculptor and ceramicist'},\n                 'position': 2,\n                 'title': None,\n                 'year': '1925'}]}\n\n\nAuthors\n=======\nDavide Brunato and others who have contributed with code or with sample cases.\n\nLicense\n=======\nThis software is distributed under the terms of the MIT License.\nSee the file 'LICENSE' in the root directory of the present\ndistribution, or http://opensource.org/licenses/MIT.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "An XML Schema validator and decoder",
    "version": "3.4.3",
    "project_urls": {
        "Homepage": "https://github.com/sissaschool/xmlschema"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5a49ff75976757b23d6345ec1021fd0d2480d7ceed66d285e31556ac1147858d",
                "md5": "a1d85643b5612076a7a1927ccc0d7695",
                "sha256": "eea4e5a1aac041b546ebe7b2eb68eb5eaebf5c5258e573cfc182375676b2e4e3"
            },
            "downloads": -1,
            "filename": "xmlschema-3.4.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a1d85643b5612076a7a1927ccc0d7695",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 417847,
            "upload_time": "2024-10-31T09:47:12",
            "upload_time_iso_8601": "2024-10-31T09:47:12.855286Z",
            "url": "https://files.pythonhosted.org/packages/5a/49/ff75976757b23d6345ec1021fd0d2480d7ceed66d285e31556ac1147858d/xmlschema-3.4.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "11ca56579e6b4558c3c06902fe6647280345da8087f080c86e85166fd2131f8d",
                "md5": "d6d9fbac420431cbb5d6f6777ac7fdab",
                "sha256": "0c638dac81c7d6c9da9a8d7544402c48cffe7ee0e13cc47fc0c18794d1395dfb"
            },
            "downloads": -1,
            "filename": "xmlschema-3.4.3.tar.gz",
            "has_sig": false,
            "md5_digest": "d6d9fbac420431cbb5d6f6777ac7fdab",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 585144,
            "upload_time": "2024-10-31T09:47:17",
            "upload_time_iso_8601": "2024-10-31T09:47:17.078190Z",
            "url": "https://files.pythonhosted.org/packages/11/ca/56579e6b4558c3c06902fe6647280345da8087f080c86e85166fd2131f8d/xmlschema-3.4.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-31 09:47:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sissaschool",
    "github_project": "xmlschema",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "xmlschema"
}
        
Elapsed time: 1.54390s