Hi Hivemind,
I'm getting an error with a few simple Python scripts for parsing XML that I'm following from a textbook. The first script "library.xml" has the contents:
Code:
<?xml version = "1.0"?>
<library owner = "John Q. Reader"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNameSpaceSchemaLocation = "library.xsd">
<book>
<title>Sandman Volume 1: Preludes and Nocturnes</title>
<author>Neil Gaiman</author>
</book>
<book>
<title>Good Omens</title>
<author>Neil Gaiman</author>
<author>Terry Pratchett</author>
</book>
<book>
<title>"Repent Harlequin" Said the Tick-Tock Man</title>
<author>Harlan Ellison</author>
</book>
</library>
---------------------------------------------------------------------------------------------------------------
The second script "library.xsd" has the contents:
Code:
<?xml version = "1.0"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema-instance">
<xs:element name = "library">
<xs:complexType>
<xs:sequence>
<xs:element name = "book" maxOccurs = "unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name = "title" type = "xs:string"/>
<xs:element name = "author" type = "xs:string" maxOccurs = "unbounded"/>
</xs:sequence>
<xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name = "owner" type = "xs:string" use = "required"/>
</xs:complexType>
</xs:element>
</xs:schema>
-----------------------------------------------------------------------------------------------------
The third script "validator.py" has the contents:
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from xml.parsers.xmlproc import xmlval
class DocErrorHandler(xmlval.ErrorHandler):
def warning(self, message):
print message
def error(self, message):
print message
def fatal(self, message):
print message
parser = xmlval.XMLValidator()
parser.set_error_handler(DocErrorHandler(parser))
parser.parse_resource("library.xml")
-----------------------------------------------------------------------------------
I'm using Python 2.6 and when I run validator.py I get the following output:
Element 'library' not declared
Element 'book' not declared
Element 'title' not declared
Element 'author' not declared
Element 'book' not declared
Element 'title' not declared
Element 'author' not declared
Element 'author' not declared
Element 'book' not declared
Element 'title' not declared
Element 'author' not declared
-------------------------------------------------------------------------------------
What's causing this problem? I've installed PyXML and it seems to be working correctly.
Thanks