A pure-Python library and command line tool for validating XML documents aginst an RELAX NG schema.
Prang runs on Python 3.4 on both CPython and PyPy. See the GitHub repository for code, bugs, feature requests etc.
Installation
It’s a good idea to set up a virtualenv:
virtualenv venv . venv/bin/activate
then install Prang with pip:
pip install prang
Quickstart
to use from the command line:
prang odf.rng content.xml
this uses the odf.rng
RELAX NG schema file to validate the content.xml
file. Any errors are written to stdout
.
To use as a library:
>>> import prang >>> >>> schema_str = """<?xml version="1.0"?> ... <element name="foo" ... xmlns="http://relaxng.org/ns/structure/1.0" ... xmlns:a="http://relaxng.org/ns/annotation/1.0" ... xmlns:ex1="http://www.example.com/n1" ... xmlns:ex2="http://www.example.com/n2"> ... <a:documentation>A foo element.</a:documentation> ... <element name="ex1:bar1"> ... <empty/> ... </element> ... <element name="ex2:bar2"> ... <empty/> ... </element> ... </element>""" >>> schema = prang.Schema(schema_str) >>> valid_doc_str = """<?xml version="1.0"?> ... <foo> ... <pre1:bar1 xmlns:pre1="http://www.example.com/n1"/> ... <pre2:bar2 xmlns:pre2="http://www.example.com/n2"/> ... </foo>""" >>> >>> schema.validate(valid_doc_str) >>> >>> >>> invalid_doc_str = """<?xml version="1.0"?> ... <bar> ... <pre1:bar1 xmlns:pre1="http://www.example.com/n1"/> ... <pre2:bar2 xmlns:pre2="http://www.example.com/n2"/> ... </bar>""" >>> try: ... schema.validate(invalid_doc_str) ... except prang.PrangException as e: ... print(e) The pattern 'NotAllowed()' doesn't match the node '/bar'.
Release Notes
Version 0.0.1, 2015-09-23
-
First working version.
Version 0.0.0, 2015-08-04
-
Initial release, nothing to see yet.