# HG changeset patch # User Paul Boddie # Date 1220142963 -7200 # Node ID b0cf845fe5a5bb950aedb54ba91b297808e37149 # Parent 12307bf68e06dc306b5509c0b4edbf598c51217d Added Schematron and XML Schema support. Updated release notes. diff -r 12307bf68e06 -r b0cf845fe5a5 README.txt --- a/README.txt Sun Aug 31 01:31:31 2008 +0200 +++ b/README.txt Sun Aug 31 02:36:03 2008 +0200 @@ -75,7 +75,8 @@ * Replaced the getElementById implementation with one based on libxml2's own support for finding attributes declared as identifiers. * Introduced support for validation, together with the libxml2dom.errors - module. + module. Relax-NG, XML Schema and Schematron are supported, depending on + libxml2 support. * Improved error messages related to parsing. * Added DOMConfiguration support to documents. diff -r 12307bf68e06 -r b0cf845fe5a5 libxml2dom/__init__.py --- a/libxml2dom/__init__.py Sun Aug 31 01:31:31 2008 +0200 +++ b/libxml2dom/__init__.py Sun Aug 31 02:36:03 2008 +0200 @@ -616,15 +616,17 @@ Validate the document against the given schema document, 'doc'. """ + validation_ns = doc.documentElement.namespaceURI + if hasattr(doc, "as_native_node"): - _schema = Document_schema(doc.as_native_node()) + _schema = Document_schema(doc.as_native_node(), validation_ns) else: - _schema = Document_schemaFromString(doc.toString()) + _schema = Document_schemaFromString(doc.toString(), validation_ns) try: self.error_handler.reset() - return Document_validate(_schema, self._node, self.error_handler) + return Document_validate(_schema, self._node, self.error_handler, validation_ns) finally: - Schema_free(_schema) + Schema_free(_schema, validation_ns) # DOM Level 3 Validation methods. diff -r 12307bf68e06 -r b0cf845fe5a5 libxml2dom/macrolib/macrolib.py --- a/libxml2dom/macrolib/macrolib.py Sun Aug 31 01:31:31 2008 +0200 +++ b/libxml2dom/macrolib/macrolib.py Sun Aug 31 02:36:03 2008 +0200 @@ -736,14 +736,43 @@ libxml2mod.xmlParseDocument(context) # Schema and validation helper functions and classes. +# NOTE: Should potentially combine these with other definitions. -def Document_schema(doc): - return Schema_parse(libxml2mod.xmlRelaxNGNewDocParserCtxt(doc)) +RELAXNG_NS = "http://relaxng.org/ns/structure/1.0" +SCHEMATRON_NS = "http://purl.oclc.org/dsdl/schematron" +XMLSCHEMA_NS = "http://www.w3.org/2001/XMLSchema" + +def Document_schema(doc, namespaceURI): + if namespaceURI == RELAXNG_NS: + return Schema_parseRelaxNG(libxml2mod.xmlRelaxNGNewDocParserCtxt(doc)) + elif namespaceURI == SCHEMATRON_NS: + return Schema_parseSchematron(libxml2mod.xmlSchematronNewDocParserCtxt(doc)) + elif namespaceURI == XMLSCHEMA_NS: + return Schema_parseSchema(libxml2mod.xmlSchemaNewDocParserCtxt(doc)) + else: + return None -def Document_schemaFromString(s): - return Schema_parse(libxml2mod.xmlRelaxNGNewMemParserCtxt(s, len(s))) +def Document_schemaFromString(s, namespaceURI): + if namespaceURI == RELAXNG_NS: + return Schema_parseRelaxNG(libxml2mod.xmlRelaxNGNewMemParserCtxt(s, len(s))) + elif namespaceURI == SCHEMATRON_NS: + return Schema_parseSchematron(libxml2mod.xmlSchematronNewMemParserCtxt(s, len(s))) + elif namespaceURI == XMLSCHEMA_NS: + return Schema_parseSchema(libxml2mod.xmlSchemaNewMemParserCtxt(s, len(s))) + else: + return None -def Document_validate(schema, doc, error_handler): +def Document_validate(schema, doc, error_handler, namespaceURI): + if namespaceURI == RELAXNG_NS: + return Document_validateRelaxNG(schema, doc, error_handler) + elif namespaceURI == SCHEMATRON_NS: + return Document_validateSchematron(schema, doc, error_handler) + elif namespaceURI == XMLSCHEMA_NS: + return Document_validateSchema(schema, doc, error_handler) + else: + return 0 + +def Document_validateRelaxNG(schema, doc, error_handler): validator_context = libxml2mod.xmlRelaxNGNewValidCtxt(schema) handler = ValidationHandler(error_handler) libxml2mod.xmlRelaxNGSetValidErrors(validator_context, handler.error, handler.warning, None) @@ -753,14 +782,51 @@ finally: libxml2mod.xmlRelaxNGFreeValidCtxt(validator_context) -def Schema_parse(context): +def Document_validateSchematron(schema, doc, error_handler): + validator_context = libxml2mod.xmlSchematronNewValidCtxt(schema) + handler = ValidationHandler(error_handler) + libxml2mod.xmlSchematronSetValidErrors(validator_context, handler.error, handler.warning, None) + try: + status = libxml2mod.xmlSchematronValidateDoc(validator_context, doc) + return status == 0 + finally: + libxml2mod.xmlSchematronFreeValidCtxt(validator_context) + +def Document_validateSchema(schema, doc, error_handler): + validator_context = libxml2mod.xmlSchemaNewValidCtxt(schema) + handler = ValidationHandler(error_handler) + libxml2mod.xmlSchemaSetValidErrors(validator_context, handler.error, handler.warning, None) + try: + status = libxml2mod.xmlSchemaValidateDoc(validator_context, doc) + return status == 0 + finally: + libxml2mod.xmlSchemaFreeValidCtxt(validator_context) + +def Schema_parseRelaxNG(context): try: return libxml2mod.xmlRelaxNGParse(context) finally: libxml2mod.xmlRelaxNGFreeParserCtxt(context) -def Schema_free(schema): - libxml2mod.xmlRelaxNGFree(schema) +def Schema_parseSchematron(context): + try: + return libxml2mod.xmlSchematronParse(context) + finally: + libxml2mod.xmlSchematronFreeParserCtxt(context) + +def Schema_parseSchema(context): + try: + return libxml2mod.xmlSchemaParse(context) + finally: + libxml2mod.xmlSchemaFreeParserCtxt(context) + +def Schema_free(schema, namespaceURI): + if namespaceURI == RELAXNG_NS: + libxml2mod.xmlRelaxNGFree(schema) + elif namespaceURI == SCHEMATRON_NS: + libxml2mod.xmlSchematronFree(schema) + elif namespaceURI == XMLSCHEMA_NS: + libxml2mod.xmlSchemaFree(schema) class ValidationHandler: diff -r 12307bf68e06 -r b0cf845fe5a5 packages/debian-etch/python-libxml2dom/debian/changelog --- a/packages/debian-etch/python-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 +++ b/packages/debian-etch/python-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 @@ -9,7 +9,8 @@ based on libxml2's own support for finding attributes declared as identifiers. * Introduced support for validation, together with the - libxml2dom.errors module. + libxml2dom.errors module. Relax-NG, XML Schema and + Schematron are supported, depending on libxml2 support. * Improved error messages related to parsing. * Added DOMConfiguration support to documents. diff -r 12307bf68e06 -r b0cf845fe5a5 packages/debian-sarge/python2.3-libxml2dom/debian/changelog --- a/packages/debian-sarge/python2.3-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 +++ b/packages/debian-sarge/python2.3-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 @@ -9,7 +9,8 @@ based on libxml2's own support for finding attributes declared as identifiers. * Introduced support for validation, together with the - libxml2dom.errors module. + libxml2dom.errors module. Relax-NG, XML Schema and + Schematron are supported, depending on libxml2 support. * Improved error messages related to parsing. * Added DOMConfiguration support to documents. diff -r 12307bf68e06 -r b0cf845fe5a5 packages/ubuntu-feisty/python-libxml2dom/debian/changelog --- a/packages/ubuntu-feisty/python-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 +++ b/packages/ubuntu-feisty/python-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 @@ -9,7 +9,8 @@ based on libxml2's own support for finding attributes declared as identifiers. * Introduced support for validation, together with the - libxml2dom.errors module. + libxml2dom.errors module. Relax-NG, XML Schema and + Schematron are supported, depending on libxml2 support. * Improved error messages related to parsing. * Added DOMConfiguration support to documents. diff -r 12307bf68e06 -r b0cf845fe5a5 packages/ubuntu-hoary/python2.4-libxml2dom/debian/changelog --- a/packages/ubuntu-hoary/python2.4-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 +++ b/packages/ubuntu-hoary/python2.4-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 @@ -9,7 +9,8 @@ based on libxml2's own support for finding attributes declared as identifiers. * Introduced support for validation, together with the - libxml2dom.errors module. + libxml2dom.errors module. Relax-NG, XML Schema and + Schematron are supported, depending on libxml2 support. * Improved error messages related to parsing. * Added DOMConfiguration support to documents. diff -r 12307bf68e06 -r b0cf845fe5a5 tests/test_invalid_schema.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_invalid_schema.xml Sun Aug 31 02:36:03 2008 +0200 @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff -r 12307bf68e06 -r b0cf845fe5a5 tests/test_invalid_schematron.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_invalid_schematron.xml Sun Aug 31 02:36:03 2008 +0200 @@ -0,0 +1,10 @@ + + + Test of Schematron validation + + + A zoo contains cages each with an id attribute + Each cage has a description attribute + + + diff -r 12307bf68e06 -r b0cf845fe5a5 tests/test_valid_schema.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_valid_schema.py Sun Aug 31 02:36:03 2008 +0200 @@ -0,0 +1,17 @@ +#!/usr/bin/env python + +import libxml2dom + +schema = libxml2dom.parse("tests/test_valid_schema.xml") +d = libxml2dom.parse("tests/test_valid.xml") +print d.validate(schema) +print d.validateDocument(schema) +print d.getParameter("error-handler") + +schema = libxml2dom.parse("tests/test_invalid_schema.xml") +d = libxml2dom.parse("tests/test_invalid.xml") +print d.validate(schema) +print d.validateDocument(schema) +print d.getParameter("error-handler") + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 12307bf68e06 -r b0cf845fe5a5 tests/test_valid_schema.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_valid_schema.xml Sun Aug 31 02:36:03 2008 +0200 @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff -r 12307bf68e06 -r b0cf845fe5a5 tests/test_valid_schematron.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_valid_schematron.py Sun Aug 31 02:36:03 2008 +0200 @@ -0,0 +1,17 @@ +#!/usr/bin/env python + +import libxml2dom + +schema = libxml2dom.parse("tests/test_valid_schematron.xml") +d = libxml2dom.parse("tests/test_valid.xml") +print d.validate(schema) +print d.validateDocument(schema) +print d.getParameter("error-handler") + +schema = libxml2dom.parse("tests/test_invalid_schematron.xml") +d = libxml2dom.parse("tests/test_invalid.xml") +print d.validate(schema) +print d.validateDocument(schema) +print d.getParameter("error-handler") + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 12307bf68e06 -r b0cf845fe5a5 tests/test_valid_schematron.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_valid_schematron.xml Sun Aug 31 02:36:03 2008 +0200 @@ -0,0 +1,9 @@ + + + Test of Schematron validation + + + A zoo contains cages each with an id attribute + + +