lxml.isoschematron package¶
Module contents¶
The lxml.isoschematron package implements ISO Schematron support on top
of the pure-xslt ‘skeleton’ implementation.
-
class
lxml.isoschematron.Schematron(etree=None, file=None, include=True, expand=True, include_params={}, expand_params={}, compile_params={}, store_schematron=False, store_xslt=False, store_report=False, phase=None, error_finder=//svrl:failed-assert)[source]¶ Bases:
lxml.etree._ValidatorAn ISO Schematron validator.
Pass a root Element or an ElementTree to turn it into a validator. Alternatively, pass a filename as keyword argument ‘file’ to parse from the file system.
Schematron is a less well known, but very powerful schema language. The main idea is to use the capabilities of XPath to put restrictions on the structure and the content of XML documents.
The standard behaviour is to fail on
failed-assertfindings only (ASSERTS_ONLY). To change this, you can either pass a report filter function to theerror_finderparameter (e.g.ASSERTS_AND_REPORTSor a customXPathobject), or subclass isoschematron.Schematron for complete control of the validation process.Built on the Schematron language ‘reference’ skeleton pure-xslt implementation, the validator is created as an XSLT 1.0 stylesheet using these steps:
(Extract from XML Schema or RelaxNG schema)
Process inclusions
Process abstract patterns
Compile the schematron schema to XSLT
The
includeandexpandkeyword arguments can be used to switch off steps 1) and 2). To set parameters for steps 1), 2) and 3) hand parameter dictionaries to the keyword argumentsinclude_params,expand_paramsorcompile_params. For convenience, the compile-step parameterphaseis also exposed as a keyword argumentphase. This takes precedence if the parameter is also given in the parameter dictionary.If
store_schematronis set to True, the (included-and-expanded) schematron document tree is stored and available through theschematronproperty. Ifstore_xsltis set to True, the validation XSLT document tree will be stored and can be retrieved through thevalidator_xsltproperty. Withstore_reportset to True (default: False), the resulting validation report document gets stored and can be accessed as thevalidation_reportproperty.Here is a usage example:
>>> from lxml import etree >>> from lxml.isoschematron import Schematron >>> schematron = Schematron(etree.XML(''' ... <schema xmlns="http://purl.oclc.org/dsdl/schematron" > ... <pattern id="id_only_attribute"> ... <title>id is the only permitted attribute name</title> ... <rule context="*"> ... <report test="@*[not(name()='id')]">Attribute ... <name path="@*[not(name()='id')]"/> is forbidden<name/> ... </report> ... </rule> ... </pattern> ... </schema>'''), ... error_finder=Schematron.ASSERTS_AND_REPORTS) >>> xml = etree.XML(''' ... <AAA name="aaa"> ... <BBB id="bbb"/> ... <CCC color="ccc"/> ... </AAA> ... ''') >>> schematron.validate(xml) False >>> xml = etree.XML(''' ... <AAA id="aaa"> ... <BBB id="bbb"/> ... <CCC/> ... </AAA> ... ''') >>> schematron.validate(xml) True
-
_extract(element)[source]¶ Extract embedded schematron schema from non-schematron host schema. This method will only be called by __init__ if the given schema document is not a schematron schema by itself. Must return a schematron schema document tree or None.
-
ASSERTS_AND_REPORTS= //svrl:failed-assert | //svrl:successful-report¶
-
ASSERTS_ONLY= //svrl:failed-assert¶
-
_compile= <lxml.etree.XSLT object>¶
-
_domain= 28¶
-
_error_type= 4000¶
-
_expand= <lxml.etree.XSLT object>¶
-
_extract_rng= <lxml.etree.XSLT object>¶
-
_extract_xsd= <lxml.etree.XSLT object>¶
-
_include= <lxml.etree.XSLT object>¶
-
_level= 2¶
-
_validation_errors= //svrl:failed-assert¶
-
property
schematron¶ ISO-schematron schema document (None if object has been initialized with store_schematron=False).
-
property
validation_report¶ ISO-schematron validation result report (None if result-storing has been turned off).
-
property
validator_xslt¶ ISO-schematron skeleton implementation XSLT validator document (None if object has been initialized with store_xslt=False).
-
lxml.isoschematron._stylesheet_param_dict(paramsDict, kwargsDict)[source]¶ Return a copy of paramsDict, updated with kwargsDict entries, wrapped as stylesheet arguments. kwargsDict entries with a value of None are ignored.
-
lxml.isoschematron.stylesheet_params(**kwargs)[source]¶ Convert keyword args to a dictionary of stylesheet parameters. XSL stylesheet parameters must be XPath expressions, i.e.:
string expressions, like “‘5’”
simple (number) expressions, like “5”
valid XPath expressions, like “/a/b/text()”
This function converts native Python keyword arguments to stylesheet parameters following these rules: If an arg is a string wrap it with XSLT.strparam(). If an arg is an XPath object use its path string. If arg is None raise TypeError. Else convert arg to string.