[lxml-dev] xpath: retrieve attribute names

Hi, is there a convenient way to retrieve element attribute values and *names* using xpath? I haven't yet found an XPath (1.0) expression and believe this is impossible in XPath 1.0. It is of course trivial to get at the attribute values:
root.xpath('//@*') ['TREE', 'int', 'bar', 'woop']
All I can come up with for getting at attribute names are ugly beasts like
[ (elt.xpath('name(@*[$i])', i=i+1), elt.xpath('@*[$i]', i=i+1)[0]) for i in range(elt.xpath('count(@*)')) ] [('py:pytype', 'int'), ('foo', 'bar'), ('doo', 'woop')]
I also thought about using exslt trickery, but now I'm a bit confused about its possible usage in XPath:
Seems like this isn't possible? Anyhow: How about adding a getname() method to XPath smart string results, analogous to the getparent() functionality? This would * return the (ns-qualified) attribute name for attribute results * return the (ns-qualified) element tag for element results It seems to me that having getparent() isn't of much use for attribute results if you want to get at the attribute's name. Holger -- Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief! Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail

jholg@gmx.de, 28.12.2010 17:23:
is there a convenient way to retrieve element attribute values and *names* using xpath?
As part of an XPath expression? I don't know. But see below.
[ (elt.xpath('name(@*[$i])', i=i+1), elt.xpath('@*[$i]', i=i+1)[0]) for i in range(elt.xpath('count(@*)')) ] [('py:pytype', 'int'), ('foo', 'bar'), ('doo', 'woop')]
I didn't benchmark it, but I have my doubts that this is faster than [ el.attrib for el in root.xpath('//*[@*]') ] It's certainly less readable.
I haven't used these in a while. Don't you have to map the prefixes to their namespaces? (although I'd expect an exception if they are undefined).
Since 2.3, there is an "attrname" property on the smart string results of attribute values. Element text results don't need this as you can pass through getparent(). http://codespeak.net/lxml/dev/api/lxml.etree._ElementUnicodeResult-class.htm... Stefan

Hi Stefan,
Definitely.
Looks like these namespaces are automagically recognized. But I just realized that maybe this doesn't work due to my libxslt version. From the 2.3alpha1 change log: * During regular XPath evaluation, various ESXLT functions are available within their namespace when using libxslt 1.1.26 or later. I'm running on libxslt 1.1.23.
Ah, didn't spot this. Just what I was looking for! Holger -- Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief! Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail

jholg@gmx.de, 28.12.2010 17:23:
is there a convenient way to retrieve element attribute values and *names* using xpath?
As part of an XPath expression? I don't know. But see below.
[ (elt.xpath('name(@*[$i])', i=i+1), elt.xpath('@*[$i]', i=i+1)[0]) for i in range(elt.xpath('count(@*)')) ] [('py:pytype', 'int'), ('foo', 'bar'), ('doo', 'woop')]
I didn't benchmark it, but I have my doubts that this is faster than [ el.attrib for el in root.xpath('//*[@*]') ] It's certainly less readable.
I haven't used these in a while. Don't you have to map the prefixes to their namespaces? (although I'd expect an exception if they are undefined).
Since 2.3, there is an "attrname" property on the smart string results of attribute values. Element text results don't need this as you can pass through getparent(). http://codespeak.net/lxml/dev/api/lxml.etree._ElementUnicodeResult-class.htm... Stefan

Hi Stefan,
Definitely.
Looks like these namespaces are automagically recognized. But I just realized that maybe this doesn't work due to my libxslt version. From the 2.3alpha1 change log: * During regular XPath evaluation, various ESXLT functions are available within their namespace when using libxslt 1.1.26 or later. I'm running on libxslt 1.1.23.
Ah, didn't spot this. Just what I was looking for! Holger -- Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief! Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail
participants (2)
-
jholg@gmx.de
-
Stefan Behnel