[XML-SIG] namespace headache
Uche Ogbuji
uogbuji@fourthought.com
Tue, 02 May 2000 12:33:18 -0600
> reading the XML namespace specification makes my brain
> hurt, so I thought I'd ask here before it explodes...
>
> given the following XML snippet, what's the correct namespace
> for the "attribute" attribute?
>
> <ns:body xmlns:ns='namespace:'>
> <ns:member attribute='value'>
> </ns:member>
> </ns:body>
>
> I'm not smart enough to figure that out from the specification,
> my intuition says "no namespace", and so does James Clark's
> namespace note (http://www.jclark.com/xml/xmlns.htm) where
[snip]
> class Parser(xmllib.XMLParser):
> def unknown_starttag(self, tag, attr):
> print "S", repr(tag), attr
> def unknown_endtag(self, tag):
> print "E", repr(tag)
>
> p = Parser()
> p.feed("""
> <ns:body xmlns:ns='namespace:'>
> <ns:member attribute='value'>
> </ns:member>
> </ns:body>
> """)
> p.close()
>
> gives the following output:
>
> S 'namespace: body' {}
> S 'namespace: member' {'namespace: attribute': 'value'}
> E 'namespace: member'
> E 'namespace: body'
>
> instead of
>
> S 'namespace: body' {}
> S 'namespace: member' {'attribute': 'value'}
> E 'namespace: member'
> E 'namespace: body'
>
> can anyone sort this out for me?
Easy enough. Your instinct (and Mr. Clark) are right and xmllib is wrong.
> (and no, I really have to be able to use xmllib, to make sure
> soaplib.py works under an off-the-shelf Python distribution...)
So the next part of my comment is of no use to you but I'll make it anyway:
4DOM does get it right:
[uogbuji@borgia uogbuji]$ python
Python 1.5.2 (#1, Mar 21 2000, 18:17:19) [GCC 2.95.3 19991030 (prerelease)]
on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from Ft.Dom.Ext.Reader import Sax2
>>> source = """<ns:body xmlns:ns='namespace:'>
... <ns:member attribute='value'>
... </ns:member>
... </ns:body>"""
>>> doc = Sax2.FromXml(source)
>>> member = doc.documentElement.childNodes[1]
>>> member
<Element Node at 135665768: Name = 'ns:member' with 1 attributes and 1
children>
>>> attr = member.attributes[0]
>>> attr
<Attribute Node at 134895008: Name = "attribute", Value = "value">
>>> att.namespaceURI
>>> attr.localName
'attribute'
>>> attr.nodeName
'attribute'
>>> import Ft.Dom.Ext
>>> Ft.Dom.Ext.GetAllNs(attr)
{'ns': 'namespace:', 'xml': 'http://www.w3.org/XML/1998/namespace'}
>>>
Hmm. I just noticed that "1 attributes and 1 children". Silliness. I'll
sort that out...
--
Uche Ogbuji Senior Software Engineer
uche.ogbuji@fourthought.com +01 303 583 9900 x 101
Fourthought, Inc. http://Fourthought.com
4735 East Walnut St, Ste. C, Boulder, CO 80301-9036, USA
Software-engineering, knowledge-management, XML, CORBA, Linux, Python