[XML-SIG] Getting namespace aware parser to work...

Clark C. Evans cce@clarkevans.com
Mon, 19 Mar 2001 06:03:37 -0500 (EST)


I'm trying to process the following xml file, with
this python script to strip all elements with a 
given namespace.  I believe that I have a pretty
recent version (0.5.2).  I get the error following...

-----------------------------------------------------------------
test.xml
-----------------------------------------------------------------

<test>
  <one xmlns="baduri">strip</one>
  <two>keep</two>
</test>     

-----------------------------------------------------------------
test.py
-----------------------------------------------------------------

"""Strips a particular namespace from an XML document."""
from xml.sax import saxutils

class StripperFilter(saxutils.XMLFilterBase ):
    """Does the actual stripping"""
    def __init__(self,nmsp):
        """The namespace to strip is nmsp"""
        saxutils.XMLFilterBase.__init__(self)
        self.nmsp = nmsp
        
    def startElementNS(self, name, qname, attrs):
        """Ignores elements and strips attributes of nmsp"""
        if name[0] != self.nmsp:
            #
            # Warning: For efficiency this dives into the
            #          underlying representation of AttributesNSImpl
            #          and deletes attributes to be stripped.
            #
            #  _attrs should be of the form {(ns_uri, lname): value, ...}.
            #  _qnames of the form {(ns_uri, lname): qname, ...}."""
            #
            for (ns_uri,lname) in attrs._attrs.keys():
                if nmsp == ns_uri: del attrs._attrs[(ns_uri,lname)]
            saxutils.XMLFilterBase.startElementNS(self,name,qname,attrs)


from xml.sax import make_parser
from xml.sax.handler import feature_namespaces

def testStripper():
    parser = make_parser()
    parser.setFeature(feature_namespaces, 1)
    strip = StripperFilter('myuri')
    out = saxutils.XMLGenerator()
    strip.setContentHandler(out)
    parser.setContentHandler(strip)
    parser.parse("c:\\work\\xfld\\test.xml")

if __name__ == '__main__':
    testStripper()

----------------------------------------------------------------------
The error message
----------------------------------------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<test>
  stripTraceback (most recent call last):
  File "<stdin>", line 40, in ?
  File "<stdin>", line 37, in testStripper
  File "F:\Program Files\Python\_xmlplus\sax\expatreader.py", line 43, in
parse
    xmlreader.IncrementalParser.parse(self, source)
  File "F:\Program Files\Python\_xmlplus\sax\xmlreader.py", line 120, in
parse
    self.feed(buffer)
  File "F:\Program Files\Python\_xmlplus\sax\expatreader.py", line 87, in
feed
    self._parser.Parse(data, isFinal)
  File "F:\Program Files\Python\_xmlplus\sax\expatreader.py", line 187, in
end_element_ns
    self._cont_handler.endElementNS(pair, None)
  File "F:\Program Files\Python\_xmlplus\sax\saxutils.py", line 259, in
endElementNS
    self._cont_handler.endElementNS(name, qname)
  File "F:\Program Files\Python\_xmlplus\sax\saxutils.py", line 192, in
endElementNS
    qname = self._current_context[name[0]] + ":" + name[1]
TypeError: bad operand type(s) for +