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

Class Schematron

object --+    
         |    
_Validator --+
             |
            Schematron

Schematron(self, etree=None, file=None) A 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. Here is a simple example:

>>> schematron = etree.Schematron(etree.XML('''
... <schema xmlns="http://www.ascc.net/xml/schematron" >
...   <pattern name="id is the only permited attribute name">
...     <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

Schematron was added to libxml2 in version 2.6.21. Before version 2.6.32, however, Schematron lacked support for error reporting other than to stderr. This version is therefore required to retrieve validation warnings and errors in lxml.

Instance Methods [hide private]
 
__call__(self, etree)
Validate doc using Schematron.
 
__init__(self, etree=None, file=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
a new object with type S, a subtype of T
__new__(T, S, ...)

Inherited from _Validator: assertValid, assert_, validate

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from _Validator: error_log

Inherited from object: __class__

Method Details [hide private]

__call__(self, etree)
(Call operator)

 

Validate doc using Schematron.

Returns true if document is valid, false if not.

__init__(self, etree=None, file=None)
(Constructor)

 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__

__new__(T, S, ...)

 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__