[XML-SIG] [SOLVED] soapy hacking -- getAttributeNS
Uche Ogbuji
uche.ogbuji@fourthought.com
Sat, 01 Mar 2003 16:42:48 -0700
> On Wednesday 26 February 2003 10:48 pm, Mark Bucciarelli wrote:
>
> > Stupid question ... what's the proper way to retrieve the value of the name
> > attribute using pnode.getAttributeNS()?
>
> Nope. To get at an attribute without a namespace prefix, you must use
> getAttribute, not getAttributeNS.
Noooooooooo! :-)
Please do not mix up getAttribute and getAttributeNS. If you use namespaces,
stick to the latter.
Your problem, as I mentioned, was that you should have been using
xml.dom.EMPTY_NAMESPACE, which is None, not ''.
> Another interesting thing I found is if an attribute does have a prefix, then
> the xmlns declaration must be in a parent tag--it cannot be in the same tag
> as the attribute.
Hmm? Not true at all.
> So, there is no way to get the value of the name attribute from the following
> xml:
>
> s = "<root><test xmlns:mb='http://123' mb:name='abc' /></root>"
>
> Maybe this isn't legal xml.
Course it is, and works just fine for me. If you insist on using 4DOM:
>>> from xml.dom.ext.reader import Sax2
>>> reader = Sax2.Reader()
>>> doc = reader.fromString("<root><test xmlns:mb='http://123' mb:name='abc' />
</root>")
>>> test_node = doc.documentElement.firstChild
>>> test_node.getAttributeNS(u'http://123', u'name')
u'abc'
BTW, using 4Suite's Domlette, which is *much* faster than 4DOM you would use:
>>> from Ft.Xml.Domlette import NonvalidatingReader
>>> doc = NonvalidatingReader.parseString("<root><test xmlns:mb='http://123'
mb:name='abc' /></root>","file:bogus.xml")
>>> test_node = doc.documentElement.firstChild
>>> test_node.attributes[(u'http://123', u'name')].value
u'abc'
May I suggest my Python/XML articles? They cover some of the issues you've
run into:
http://www.xml.com/pub/au/84
There is also a lot of relevant stuff on my Akara site:
http://uche.ogbuji.net/tech/akara/nodes/2003-01-01/pyxml-akara
--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://4Suite.org http://fourthought.com
The open office file format - http://www-106.ibm.com/developerworks/xml/library/x-think15/
4Suite Repository Features - https://www6.software.ibm.com/reg/devworks/dw-x4suite5-i/
XML class warfare - http://www.adtmag.com/article.asp?id=6965
See you at XML Web Services One - http://www.xmlconference.com/santaclara/