XML Schema Validation with Python, MSXML and comtypes
English, Python, Python Tips, Windows, XML Add commentsThe previous Python Tip, XML Schema Validation with Python, MSXML and PyWin32, described how to use PyWin32 and MSXML to validate XML contents against an XML Schema. The adaptation to perform the same with comtypes instead of PyWin32 is a simple translation.
comtypes is a pure Python COM package built on top of ctypes FFI (Foreign Function Interface). comtypes and ctypes are both developed by Thomas Heller.
Requirements
- Python is the first requirement, preferably Python 2.5, as it already includes ctypes (see below).
- Download and install comtypes.
- The ctypes library is only needed for Python 2.3 or 2.4. Python 2.5 already ships with ctypes.
- Download and install MSXML 6.0. MSXML 4.0 would also work. MSXML 3.0 does not support XML Schema.
- Optional: py_tips_msxml_val_comtypes_1.0.zip (3KB), code source and files used to illustrate this article and available under the MIT License.
Sandbox
The XML files and the XML Schema files used are respectively:
- books.xsd (XML Schema)
- books.xml (Valid XML)
- books_error.xml (Non valid XML)
The logic is similar to the one observed with PyWin32 (see XML Schema Validation with Python, MSXML and PyWin32 for more details). The code below, in the Python command line, shows how to validate an XML document with comtypes and MSXML.
ActivePython 2.5.1.1 (ActiveState Software Inc.) based on
Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from comtypes.client import CreateObject
>>> dom = CreateObject('Msxml2.DOMDocument.6.0')
>>> dom.async = 0
>>> schemas = CreateObject('Msxml2.XMLSchemaCache.6.0')
>>> schemas.add('http://www.burgaud.com/XMLSchema', 'books.xsd')
0
>>> dom.schemas = schemas
>>> dom.load('books.xml')
True
>>> dom.load('books_error.xml')
False
>>> dom.parseError.reason
u"pattern constraint failed.\r\nThe attribute: 'isbn' has an invalid value according to its data typ
e.\r\n"
>>>
...
Complete Example
You can view the listing online: msxml_schema_val_comtypes.py.
The main difference with the version using PyWin32 is the usage of comtypes.client.CreateObject instead of from win32com.client.Dispatch. The exception handling is also slightly different.
This script takes 2 parameters (XML file and XML Schema file). Here is an example of execution with the valid XML file:
C:\prompt>python msxml_schema_val.py books.xml books.xsd MSXML version 6: OK Namespace : http://www.burgaud.com/XMLSchema Schema : books.xsd Valid XML : books.xml
And an example with the non valid XML:
MSXML version 6: OK
books_error.xml: Validation Error
- Error Code : -1072897687
- Reason : '123' violates pattern constraint of '[0-9]{10}'.
The attribute 'isbn' with value '123' failed to parse.
- Character : 534
- Line : 14
- Column : 20
- Source : <Book isbn="123">
^
Recommended Books
Resources
- The Official Website of the Python Programming Language is the primary recommended resource for anything Python.
- Thomas Heller is the developer of ctypes and comtypes.
- Resources specific to MSXML:
- MSXML SDK on MSDN
- Download MSXML 6.0 SP1 (recommended)
- Download MSXML 4.0 SP2
Download
- Companion Files Version 1.0: py_tips_msxml_val_comtypes_1.0.zip (3KB)
History
- 02/08/2008: Initial publication of this blog post.
Recent Comments