1.1 --- a/README.txt Sun Aug 31 01:31:31 2008 +0200 1.2 +++ b/README.txt Sun Aug 31 02:36:03 2008 +0200 1.3 @@ -75,7 +75,8 @@ 1.4 * Replaced the getElementById implementation with one based on libxml2's 1.5 own support for finding attributes declared as identifiers. 1.6 * Introduced support for validation, together with the libxml2dom.errors 1.7 - module. 1.8 + module. Relax-NG, XML Schema and Schematron are supported, depending on 1.9 + libxml2 support. 1.10 * Improved error messages related to parsing. 1.11 * Added DOMConfiguration support to documents. 1.12
2.1 --- a/libxml2dom/__init__.py Sun Aug 31 01:31:31 2008 +0200 2.2 +++ b/libxml2dom/__init__.py Sun Aug 31 02:36:03 2008 +0200 2.3 @@ -616,15 +616,17 @@ 2.4 Validate the document against the given schema document, 'doc'. 2.5 """ 2.6 2.7 + validation_ns = doc.documentElement.namespaceURI 2.8 + 2.9 if hasattr(doc, "as_native_node"): 2.10 - _schema = Document_schema(doc.as_native_node()) 2.11 + _schema = Document_schema(doc.as_native_node(), validation_ns) 2.12 else: 2.13 - _schema = Document_schemaFromString(doc.toString()) 2.14 + _schema = Document_schemaFromString(doc.toString(), validation_ns) 2.15 try: 2.16 self.error_handler.reset() 2.17 - return Document_validate(_schema, self._node, self.error_handler) 2.18 + return Document_validate(_schema, self._node, self.error_handler, validation_ns) 2.19 finally: 2.20 - Schema_free(_schema) 2.21 + Schema_free(_schema, validation_ns) 2.22 2.23 # DOM Level 3 Validation methods. 2.24
3.1 --- a/libxml2dom/macrolib/macrolib.py Sun Aug 31 01:31:31 2008 +0200 3.2 +++ b/libxml2dom/macrolib/macrolib.py Sun Aug 31 02:36:03 2008 +0200 3.3 @@ -736,14 +736,43 @@ 3.4 libxml2mod.xmlParseDocument(context) 3.5 3.6 # Schema and validation helper functions and classes. 3.7 +# NOTE: Should potentially combine these with other definitions. 3.8 3.9 -def Document_schema(doc): 3.10 - return Schema_parse(libxml2mod.xmlRelaxNGNewDocParserCtxt(doc)) 3.11 +RELAXNG_NS = "http://relaxng.org/ns/structure/1.0" 3.12 +SCHEMATRON_NS = "http://purl.oclc.org/dsdl/schematron" 3.13 +XMLSCHEMA_NS = "http://www.w3.org/2001/XMLSchema" 3.14 + 3.15 +def Document_schema(doc, namespaceURI): 3.16 + if namespaceURI == RELAXNG_NS: 3.17 + return Schema_parseRelaxNG(libxml2mod.xmlRelaxNGNewDocParserCtxt(doc)) 3.18 + elif namespaceURI == SCHEMATRON_NS: 3.19 + return Schema_parseSchematron(libxml2mod.xmlSchematronNewDocParserCtxt(doc)) 3.20 + elif namespaceURI == XMLSCHEMA_NS: 3.21 + return Schema_parseSchema(libxml2mod.xmlSchemaNewDocParserCtxt(doc)) 3.22 + else: 3.23 + return None 3.24 3.25 -def Document_schemaFromString(s): 3.26 - return Schema_parse(libxml2mod.xmlRelaxNGNewMemParserCtxt(s, len(s))) 3.27 +def Document_schemaFromString(s, namespaceURI): 3.28 + if namespaceURI == RELAXNG_NS: 3.29 + return Schema_parseRelaxNG(libxml2mod.xmlRelaxNGNewMemParserCtxt(s, len(s))) 3.30 + elif namespaceURI == SCHEMATRON_NS: 3.31 + return Schema_parseSchematron(libxml2mod.xmlSchematronNewMemParserCtxt(s, len(s))) 3.32 + elif namespaceURI == XMLSCHEMA_NS: 3.33 + return Schema_parseSchema(libxml2mod.xmlSchemaNewMemParserCtxt(s, len(s))) 3.34 + else: 3.35 + return None 3.36 3.37 -def Document_validate(schema, doc, error_handler): 3.38 +def Document_validate(schema, doc, error_handler, namespaceURI): 3.39 + if namespaceURI == RELAXNG_NS: 3.40 + return Document_validateRelaxNG(schema, doc, error_handler) 3.41 + elif namespaceURI == SCHEMATRON_NS: 3.42 + return Document_validateSchematron(schema, doc, error_handler) 3.43 + elif namespaceURI == XMLSCHEMA_NS: 3.44 + return Document_validateSchema(schema, doc, error_handler) 3.45 + else: 3.46 + return 0 3.47 + 3.48 +def Document_validateRelaxNG(schema, doc, error_handler): 3.49 validator_context = libxml2mod.xmlRelaxNGNewValidCtxt(schema) 3.50 handler = ValidationHandler(error_handler) 3.51 libxml2mod.xmlRelaxNGSetValidErrors(validator_context, handler.error, handler.warning, None) 3.52 @@ -753,14 +782,51 @@ 3.53 finally: 3.54 libxml2mod.xmlRelaxNGFreeValidCtxt(validator_context) 3.55 3.56 -def Schema_parse(context): 3.57 +def Document_validateSchematron(schema, doc, error_handler): 3.58 + validator_context = libxml2mod.xmlSchematronNewValidCtxt(schema) 3.59 + handler = ValidationHandler(error_handler) 3.60 + libxml2mod.xmlSchematronSetValidErrors(validator_context, handler.error, handler.warning, None) 3.61 + try: 3.62 + status = libxml2mod.xmlSchematronValidateDoc(validator_context, doc) 3.63 + return status == 0 3.64 + finally: 3.65 + libxml2mod.xmlSchematronFreeValidCtxt(validator_context) 3.66 + 3.67 +def Document_validateSchema(schema, doc, error_handler): 3.68 + validator_context = libxml2mod.xmlSchemaNewValidCtxt(schema) 3.69 + handler = ValidationHandler(error_handler) 3.70 + libxml2mod.xmlSchemaSetValidErrors(validator_context, handler.error, handler.warning, None) 3.71 + try: 3.72 + status = libxml2mod.xmlSchemaValidateDoc(validator_context, doc) 3.73 + return status == 0 3.74 + finally: 3.75 + libxml2mod.xmlSchemaFreeValidCtxt(validator_context) 3.76 + 3.77 +def Schema_parseRelaxNG(context): 3.78 try: 3.79 return libxml2mod.xmlRelaxNGParse(context) 3.80 finally: 3.81 libxml2mod.xmlRelaxNGFreeParserCtxt(context) 3.82 3.83 -def Schema_free(schema): 3.84 - libxml2mod.xmlRelaxNGFree(schema) 3.85 +def Schema_parseSchematron(context): 3.86 + try: 3.87 + return libxml2mod.xmlSchematronParse(context) 3.88 + finally: 3.89 + libxml2mod.xmlSchematronFreeParserCtxt(context) 3.90 + 3.91 +def Schema_parseSchema(context): 3.92 + try: 3.93 + return libxml2mod.xmlSchemaParse(context) 3.94 + finally: 3.95 + libxml2mod.xmlSchemaFreeParserCtxt(context) 3.96 + 3.97 +def Schema_free(schema, namespaceURI): 3.98 + if namespaceURI == RELAXNG_NS: 3.99 + libxml2mod.xmlRelaxNGFree(schema) 3.100 + elif namespaceURI == SCHEMATRON_NS: 3.101 + libxml2mod.xmlSchematronFree(schema) 3.102 + elif namespaceURI == XMLSCHEMA_NS: 3.103 + libxml2mod.xmlSchemaFree(schema) 3.104 3.105 class ValidationHandler: 3.106
4.1 --- a/packages/debian-etch/python-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 4.2 +++ b/packages/debian-etch/python-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 4.3 @@ -9,7 +9,8 @@ 4.4 based on libxml2's own support for finding attributes 4.5 declared as identifiers. 4.6 * Introduced support for validation, together with the 4.7 - libxml2dom.errors module. 4.8 + libxml2dom.errors module. Relax-NG, XML Schema and 4.9 + Schematron are supported, depending on libxml2 support. 4.10 * Improved error messages related to parsing. 4.11 * Added DOMConfiguration support to documents. 4.12
5.1 --- a/packages/debian-sarge/python2.3-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 5.2 +++ b/packages/debian-sarge/python2.3-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 5.3 @@ -9,7 +9,8 @@ 5.4 based on libxml2's own support for finding attributes 5.5 declared as identifiers. 5.6 * Introduced support for validation, together with the 5.7 - libxml2dom.errors module. 5.8 + libxml2dom.errors module. Relax-NG, XML Schema and 5.9 + Schematron are supported, depending on libxml2 support. 5.10 * Improved error messages related to parsing. 5.11 * Added DOMConfiguration support to documents. 5.12
6.1 --- a/packages/ubuntu-feisty/python-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 6.2 +++ b/packages/ubuntu-feisty/python-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 6.3 @@ -9,7 +9,8 @@ 6.4 based on libxml2's own support for finding attributes 6.5 declared as identifiers. 6.6 * Introduced support for validation, together with the 6.7 - libxml2dom.errors module. 6.8 + libxml2dom.errors module. Relax-NG, XML Schema and 6.9 + Schematron are supported, depending on libxml2 support. 6.10 * Improved error messages related to parsing. 6.11 * Added DOMConfiguration support to documents. 6.12
7.1 --- a/packages/ubuntu-hoary/python2.4-libxml2dom/debian/changelog Sun Aug 31 01:31:31 2008 +0200 7.2 +++ b/packages/ubuntu-hoary/python2.4-libxml2dom/debian/changelog Sun Aug 31 02:36:03 2008 +0200 7.3 @@ -9,7 +9,8 @@ 7.4 based on libxml2's own support for finding attributes 7.5 declared as identifiers. 7.6 * Introduced support for validation, together with the 7.7 - libxml2dom.errors module. 7.8 + libxml2dom.errors module. Relax-NG, XML Schema and 7.9 + Schematron are supported, depending on libxml2 support. 7.10 * Improved error messages related to parsing. 7.11 * Added DOMConfiguration support to documents. 7.12
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/tests/test_invalid_schema.xml Sun Aug 31 02:36:03 2008 +0200 8.3 @@ -0,0 +1,23 @@ 8.4 +<?xml version="1.0"?> 8.5 +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 8.6 +<!-- XML Schema: an exercise in baroque excess --> 8.7 +<!-- See: http://www.w3.org/TR/xmlschema-0/#complexTfromSimpleT --> 8.8 + 8.9 + <xsd:element name="zoo" type="zooType"/> 8.10 + 8.11 + <xsd:complexType name="zooType"> 8.12 + <xsd:sequence> 8.13 + <xsd:element name="cage" minOccurs="0" maxOccurs="unbounded"> 8.14 + <xsd:complexType> 8.15 + <xsd:simpleContent> 8.16 + <xsd:extension base="xsd:string"> 8.17 + <xsd:attribute name="id" type="xsd:string" use="required"/> 8.18 + <xsd:attribute name="description" type="xsd:string" use="required"/> 8.19 + </xsd:extension> 8.20 + </xsd:simpleContent> 8.21 + </xsd:complexType> 8.22 + </xsd:element> 8.23 + </xsd:sequence> 8.24 + </xsd:complexType> 8.25 + 8.26 +</xsd:schema>
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/tests/test_invalid_schematron.xml Sun Aug 31 02:36:03 2008 +0200 9.3 @@ -0,0 +1,10 @@ 9.4 +<?xml version="1.0"?> 9.5 +<schema xmlns="http://purl.oclc.org/dsdl/schematron"> 9.6 + <title>Test of Schematron validation</title> 9.7 + <pattern> 9.8 + <rule context="zoo"> 9.9 + <assert test="cage[@id]">A zoo contains cages each with an id attribute</assert> 9.10 + <assert test="cage[@description]">Each cage has a description attribute</assert> 9.11 + </rule> 9.12 + </pattern> 9.13 +</schema>
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/tests/test_valid_schema.py Sun Aug 31 02:36:03 2008 +0200 10.3 @@ -0,0 +1,17 @@ 10.4 +#!/usr/bin/env python 10.5 + 10.6 +import libxml2dom 10.7 + 10.8 +schema = libxml2dom.parse("tests/test_valid_schema.xml") 10.9 +d = libxml2dom.parse("tests/test_valid.xml") 10.10 +print d.validate(schema) 10.11 +print d.validateDocument(schema) 10.12 +print d.getParameter("error-handler") 10.13 + 10.14 +schema = libxml2dom.parse("tests/test_invalid_schema.xml") 10.15 +d = libxml2dom.parse("tests/test_invalid.xml") 10.16 +print d.validate(schema) 10.17 +print d.validateDocument(schema) 10.18 +print d.getParameter("error-handler") 10.19 + 10.20 +# vim: tabstop=4 expandtab shiftwidth=4
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/tests/test_valid_schema.xml Sun Aug 31 02:36:03 2008 +0200 11.3 @@ -0,0 +1,22 @@ 11.4 +<?xml version="1.0"?> 11.5 +<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 11.6 +<!-- XML Schema: an exercise in baroque excess --> 11.7 +<!-- See: http://www.w3.org/TR/xmlschema-0/#complexTfromSimpleT --> 11.8 + 11.9 + <xsd:element name="zoo" type="zooType"/> 11.10 + 11.11 + <xsd:complexType name="zooType"> 11.12 + <xsd:sequence> 11.13 + <xsd:element name="cage" minOccurs="0" maxOccurs="unbounded"> 11.14 + <xsd:complexType> 11.15 + <xsd:simpleContent> 11.16 + <xsd:extension base="xsd:string"> 11.17 + <xsd:attribute name="id" type="xsd:string" use="required"/> 11.18 + </xsd:extension> 11.19 + </xsd:simpleContent> 11.20 + </xsd:complexType> 11.21 + </xsd:element> 11.22 + </xsd:sequence> 11.23 + </xsd:complexType> 11.24 + 11.25 +</xsd:schema>
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/tests/test_valid_schematron.py Sun Aug 31 02:36:03 2008 +0200 12.3 @@ -0,0 +1,17 @@ 12.4 +#!/usr/bin/env python 12.5 + 12.6 +import libxml2dom 12.7 + 12.8 +schema = libxml2dom.parse("tests/test_valid_schematron.xml") 12.9 +d = libxml2dom.parse("tests/test_valid.xml") 12.10 +print d.validate(schema) 12.11 +print d.validateDocument(schema) 12.12 +print d.getParameter("error-handler") 12.13 + 12.14 +schema = libxml2dom.parse("tests/test_invalid_schematron.xml") 12.15 +d = libxml2dom.parse("tests/test_invalid.xml") 12.16 +print d.validate(schema) 12.17 +print d.validateDocument(schema) 12.18 +print d.getParameter("error-handler") 12.19 + 12.20 +# vim: tabstop=4 expandtab shiftwidth=4
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/tests/test_valid_schematron.xml Sun Aug 31 02:36:03 2008 +0200 13.3 @@ -0,0 +1,9 @@ 13.4 +<?xml version="1.0"?> 13.5 +<schema xmlns="http://purl.oclc.org/dsdl/schematron"> 13.6 + <title>Test of Schematron validation</title> 13.7 + <pattern> 13.8 + <rule context="zoo"> 13.9 + <assert test="cage[@id]">A zoo contains cages each with an id attribute</assert> 13.10 + </rule> 13.11 + </pattern> 13.12 +</schema>