lxml.isoschematron.Schematron

Package lxml :: Package isoschematron :: Class Schematron |
[hide private]
[frames] | no frames]

# Class Schematron

source code

          object --+
                   |
    etree._Validator --+
                       |
                      Schematron

An 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. Built on the Schematron language 'reference' skeleton pure-xslt implementation, the validator is created as an XSLT 1.0 stylesheet using these steps:

> 0. (Extract from XML Schema or RelaxNG schema)
> 1. Process inclusions
> 2. Process abstract patterns
> 3. Compile the schematron schema to XSLT

The `include` and `expand` keyword 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 arguments `include_params`, `expand_params` or `compile_params`. For convenience, the compile-step parameter `phase` is also exposed as a keyword argument `phase`. This takes precedence if the parameter is also given in the parameter dictionary. If `store_schematron` is set to True, the (included-and-expanded) schematron document tree is stored and available through the `schematron` property. If `store_xslt` is set to True, the validation XSLT document tree will be stored and can be retrieved through the `validator_xslt` property. With `store_report` set to True (default: False), the resulting validation report document gets stored and can be accessed as the `validation_report` property.

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. Here is a simple example:

    >>> from lxml import isoschematron
    >>> schematron = isoschematron.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>
    ... '''))

    >>> xml = etree.XML('''
    ... <AAA name="aaa">
    ...   <BBB id="bbb"/>
    ...   <CCC color="ccc"/>
    ... </AAA>
    ... ''')

    >>> schematron.validate(xml)
    0

    >>> xml = etree.XML('''
    ... <AAA id="aaa">
    ...   <BBB id="bbb"/>
    ...   <CCC/>
    ... </AAA>
    ... ''')

    >>> schematron.validate(xml)
    1

Instance Methods | [hide private]
_extract(self, element) 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. | source code
__init__(self, 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) x.__init__(...) initializes x; see x.__class__.__doc__ for signature | source code
__call__(self, etree) Validate doc using Schematron. | source code
Inherited from `etree._Validator`: `__new__`, `assertValid`, `assert_`, `validate` Inherited from `etree._Validator` (private): `_append_log_message`, `_clear_error_log` Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Class Variables | [hide private]
| _domain = `28`
| _level = `2`
| _error_type = `4000`
| _extract_xsd = `_etree.XSLT(_etree.parse(os.path.join(_resource`...``
| _extract_rng = `_etree.XSLT(_etree.parse(os.path.join(_resource`...``
| _include = `_etree.XSLT(_etree.parse(os.path.join(_resources_di`...``
| _expand = `_etree.XSLT(_etree.parse(os.path.join(_resources_dir`...``
| _compile = `_etree.XSLT(_etree.parse(os.path.join(_resources_di`...``
| _validation_errors = `//svrl:failed-assert`

Properties | [hide private]
| schematron ISO-schematron schema document (None if object has been initialized with store_schematron=False).
| validator_xslt ISO-schematron skeleton implementation XSLT validator document (None if object has been initialized with store_xslt=False).
| validation_report ISO-schematron validation result report (None if result-storing has been turned off).
Inherited from `etree._Validator`: `error_log` Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, 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) (Constructor) | source code
x.__init__(...) initializes x; see x.__class__.__doc__ for signature Overrides: object.__init__ (inherited documentation)

### __call__(self, etree) (Call operator) | source code
Validate doc using Schematron. Returns true if document is valid, false if not.

Class Variable Details | [hide private]

  ### _extract_xsd Value:
      _etree.XSLT(_etree.parse(os.path.join(_resources_dir, 'xsl', 'XSD2Scht
      rn.xsl')))

  ### _extract_rng Value:
      _etree.XSLT(_etree.parse(os.path.join(_resources_dir, 'xsl', 'RNG2Scht
      rn.xsl')))

  ### _include Value:
      _etree.XSLT(_etree.parse(os.path.join(_resources_dir, 'xsl', 'iso-sche
      matron-xslt1', 'iso_dsdl_include.xsl')))

  ### _expand Value:
      _etree.XSLT(_etree.parse(os.path.join(_resources_dir, 'xsl', 'iso-sche
      matron-xslt1', 'iso_abstract_expand.xsl')))

  ### _compile Value:
      _etree.XSLT(_etree.parse(os.path.join(_resources_dir, 'xsl', 'iso-sche
      matron-xslt1', 'iso_svrl_for_xslt1.xsl')))

Property Details | [hide private]

### schematron ISO-schematron schema document (None if object has been initialized with store_schematron=False). Get Method: unreachable.schematron(self) - ISO-schematron schema document (None if object has been initialized with store_schematron=False).

### validator_xslt ISO-schematron skeleton implementation XSLT validator document (None if object has been initialized with store_xslt=False). Get Method: unreachable.validator_xslt(self) - ISO-schematron skeleton implementation XSLT validator document (None if object has been initialized with store_xslt=False).

### validation_report ISO-schematron validation result report (None if result-storing has been turned off). Get Method: unreachable.validation_report(self) - ISO-schematron validation result report (None if result-storing has been turned off).