SAX: Help on processing qualified attribute values

Markus Doering wixner at web.de
Thu Mar 10 09:22:28 EST 2005


Hey,

I am trying to process XML schema documents using namespace aware SAX 
handlers. Currently I am using the default python 2.3 parser:

parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 1)


At some point I need to parse xml attributes which contain namespace 
prefixes as their value. For example:

<xs:element name="hello" type ="xs:string"/>

The default SAX parser does a good job on dealing with qualified names 
as xml tags, but is there a way I can access the internal sax mapping 
between prefixes and full namespaces to be able to parse "qualified 
attribute values"? A simple private dictionary prefix2namespace would be 
sufficient.
Or is there a way I can tell the parser to do so for me? I tried to keep 
track of namespace declarations myself with the handler, but if you use 
  namespace aware parsing startElementNS() omits those declarations from 
the resulting attribute list of that method.

Parsing the following XML bit:

     <mapping xmlns:dwc="http://www.namespacetbd.org/darwin2" />

does not produce any attribute with startElementNS()

	def startElementNS(self, name,qname,attrs):
		print "Name:%s QName=%s, Attributes=%s"%(unicode(name),unicode(qname), 
unicode(["%s=%s"%(k,v) for k,v in attrs.items()]) )

results in

Name:(None, u'mapping') QName=mapping, Attributes=[]


Should I maybe try another parser than the default one (Expat?)

Thanks for any help,
Markus


--
  Markus Döring
  Botanic Garden and Botanical Museum Berlin Dahlem,
  Dept. of Biodiversity Informatics
  http://www.bgbm.org/BioDivInf/



More information about the Python-list mailing list