xmlproc and namespaces

xmlproc now supports XML namespaces through the addition of the xml.parsers.xmlproc.namespace module. In this module there is a NamespaceFilter class which is implemented as a parser filter. This means that it is registered with the parser as an application, and that your application then registers with the filter as the real application.

The parser will then parse as normal and send parse events to the NamespaceFilter, which will do namespace transformations and pass the events on to your application.

An example

This is an example of how namespace processing can be set up:


from xml.parsers.xmlproc import xmlproc,namespace

class MyApplication(xmlproc.Application):
    pass # Add some useful stuff here

p=xmlproc.XMLProcessor()

nsf=namespace.NamespaceFilter(p)
nsf.set_application(MyApplication())
# register error handlers and all other handlers with the parser,
# not the filter

p.set_application(nsf)
p.parse_resource("foo.xml")

MyApplication will now receive parse events where all prefixed names have been processed into names consisting of the namespace URI, followed by a space and then the local part of the name. If this isn't clear to you, try using the -n option to xvcmd.py and xpcmd.py and -o x to see what the filter does.

The NamespaceFilter interface

This is the complete NamespaceFilter interface:

def __init__(self,parser):
Creates the filter and gives it a reference to the parser.
def set_application(self,app):
Gives the filter an object to send filtered events to.
def set_report_ns_attributes(self,action):
If action is set to true xmlns attributes are reported to the application. If it is set to false they are silently removed.

Last update 2000-05-11 14:20, by Lars Marius Garshol.