[lxml-dev] David Mertz' rnc2rng is non-working
One for the archive: I tried David Mertz' rnc2rng tool and it fails to parse most RNC schemas I tried. So it's absolutely unusable for lxml (or any other purpose). The little patch at the end of the mail (revision 19519:19521 in branch scoder1) contains the changes I made to implement the integration, just in case someone else finds a better converter. Stefan Index: src/lxml/tests/test_etree.py =================================================================== --- src/lxml/tests/test_etree.py (Revision 19519) +++ src/lxml/tests/test_etree.py (Revision 19521) @@ -2243,6 +2243,18 @@ self.assert_(schema.validate(tree_valid)) self.assert_(not schema.validate(tree_invalid)) + def test_relaxng_compact(self): + tree = self.parse('<a><b>B</b><c>C</c></a>') + schema = etree.RelaxNGC(''' +default namespace = "test" +element a { + element b { text }, + element c { text } +} +''') + self.assert_(schema.validate(tree)) + + def test_relaxng_invalid_schema(self): schema = self.parse('''\ <element name="a" xmlns="http://relaxng.org/ns/structure/1.0"> Index: src/lxml/etree.pyx =================================================================== --- src/lxml/etree.pyx (Revision 19519) +++ src/lxml/etree.pyx (Revision 19521) @@ -13,6 +13,7 @@ from xmlparser cimport xmlParserCtxt, xmlDict import _elementpath +from rnc import rnctree from StringIO import StringIO import sys @@ -1744,6 +1745,23 @@ raise RelaxNGValidateError, "Internal error in Relax NG validation" return ret == 0 +def RelaxNGC(schema=None, file=None): + if file is not None: + try: + schema = file.getvalue() + except AttributeError: + if isinstance(file, (str, unicode)): + file = open(file, 'r') + schema = file.read() + file.close() + else: + schema = file.read() + tokens = rnctree.token_list(schema) + root = rnctree.make_nodetree(tokens) + + etree = parse( StringIO(root.toxml()) ) + return RelaxNG(etree) + cdef class XMLSchema: """Turn a document into an XML Schema validator. """
Stefan Behnel wrote:
One for the archive:
I tried David Mertz' rnc2rng tool and it fails to parse most RNC schemas I tried. So it's absolutely unusable for lxml (or any other purpose). The little patch at the end of the mail (revision 19519:19521 in branch scoder1) contains the changes I made to implement the integration, just in case someone else finds a better converter.
Too bad; would've been a nice feature. Thanks for taking a look at it! Regards, Martijn
participants (2)
-
Martijn Faassen
-
Stefan Behnel