xml attributes question

A

Ajay

hi!

i am trying to print the keys of a NamedNodeMap which contains the
attributes of a element.
for the element,
<DATA ref="#business.name">CatalogExample</DATA>

i expect
attribs = plist[0].attributes
attribs.keys()
to print "ref" #plist[0] is the data element shown above
It however prints the following
[(None, u'ref')]

why?

thanks

cheers
 
A

Andrew Clover

Ajay said:
attribs.keys()
[(None, u'ref')]

Namespace-aware DOM implementations index attributes by the localName
*and* their namespaceURI. The 'ref' attribute isn't in a namespace
(signified by 'None') so this should be part of the key to support eg.
getAttributeNS efficiently.

Don't rely on the Python-style dictionary methods like keys() when you
are handling a NamedNodeMap. The Python DOM bindings do not guarantee
that it exists, or what it returns if it does. 4DOM behaves
differently to minidom as you can see; other implementations are
different again. Further, the results of keys(), values() and items()
are inconsistent even within single implementations.

Sticking to the standard DOM methods, one could say:

keys= [attribs.item(i).name for i in range(attribs.length)]
 
S

Sylvain Thenault

hi!

i am trying to print the keys of a NamedNodeMap which contains the
attributes of a element.
for the element,
<DATA ref="#business.name">CatalogExample</DATA>

i expect
attribs = plist[0].attributes
attribs.keys()
to print "ref" #plist[0] is the data element shown above It however prints
the following
[(None, u'ref')]

why?

because of xml namespaces. Any xml element / attribute is fully
caracterized by it's name and the namespace it belongs to. None is used to
represent the empty namespace. With the following element :

<DATA xmlns:me="mynamespace" me:ref="#business.name">...</DATA>

your sample code would have printed :
[(u'mynamespace', u'ref')]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top