Skip to content

Commit

Permalink
Merge pull request realpython#1116 from axtimhaus/patch-1
Browse files Browse the repository at this point in the history
Add section for xmlschema
  • Loading branch information
dbader authored Aug 3, 2022
2 parents 0a0b45e + db3c45b commit bcb9ff5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/scenarios/xml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,30 @@ and then you can access elements, attributes, and values like this:
xmltodict also lets you roundtrip back to XML with the unparse function,
has a streaming mode suitable for handling files that don't fit in memory,
and supports XML namespaces.

**********
xmlschema
**********

`xmlschema <https://github.com/sissaschool/xmlschema>`_ provides support for using XSD-Schemas in Python.
Unlike other XML libraries, automatic type parsing is available, so f.e. if the schema defines an element to be of type ``int``, the parsed ``dict`` will contain also an ``int`` value for that element.
Moreover the library supports automatic and explicit validation of XML documents against a schema.

.. code-block:: python
from xmlschema import XMLSchema, etree_tostring
# load a XSD schema file
schema = XMLSchema("your_schema.xsd")
# validate against the schema
schema.validate("your_file.xml")
# or
schema.is_valid("your_file.xml")
# decode a file
data = schmema.decode("your_file.xml")
# encode to string
s = etree_tostring(schema.encode(data))

0 comments on commit bcb9ff5

Please sign in to comment.