CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2009
    Posts
    11

    Question Problem with Python XML script

    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
    Last edited by LibertyOrDeath; July 12th, 2009 at 09:49 AM. Reason: Didn't use the [code] tags initially

  2. #2
    Join Date
    Jun 2009
    Posts
    11

    Re: Problem with Python XML script

    --- Removed ---
    Last edited by LibertyOrDeath; July 12th, 2009 at 09:50 AM. Reason: Didn't use the [code] tags initially

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured