Hi Pierpaolo,
> Finally I went back to a very simple example included in
> isoschematron.Schematron docstring:
>
> >>> 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
>
> Now if I run the above code I always get True, even with the invalid
> xml input. Same situation: validation_report is correct, but return
> value is True and error_log is empty.
> I'm running python 3.4.1 with lxml 3.5dev0. Same result with lxml 3.4.2.
> Am I doing something horribly wrong without realizing or is there
> actually a bug here?
It's definitely a bug in the documentation, i.e. the docstring is wrong,
probably since the dawn of lxml isoschematron times
(and I wonder why this isn't noticed by doctest...).
Anyway: The current implementation only counts failed asserts as errors,
not reports that have been triggered. This XPath is used to look for error
elements in the svrl result report:
isoschematron/__init__.py:
[...]
# svrl result accessors
svrl_validation_errors = _etree.XPath(
'//svrl:failed-assert', namespaces={'svrl': SVRL_NS})
[...]
I *think* the rationale was that reports should be usable additionally
to the validation results, but I'd need to look a bit more into this. It's
been
a while.
That said you can easily change behaviour by using a custom schematron
validator class.
Inheriting from class Schematron and overriding the _validation_errors
class attribute
in your subclass with an etree.XPath object that detects whatever you deem
an error
in the result validation report document should do the trick:
class Schematron(_etree._Validator):
[...]
# etree.XPath object that determines input document validity when applied
to
# the svrl result report; must return a list of result elements (empty if
# valid)
_validation_errors = svrl_validation_errors
There's a customization example in src/lxml/tests/test_isoschematron.py,
too.
Holger
Landesbank Baden-Wuerttemberg
Anstalt des oeffentlichen Rechts
Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz
HRA 12704
Amtsgericht Stuttgart
_________________________________________________________________
Mailing list for the lxml Python XML toolkit - http://lxml.de/
lxml@lxml.de
https://mailman-mail5.webfaction.com/listinfo/lxml