[lxml-dev] Type returned by xpath
Hi, I want to be able to do something like this with lxml for attr in doc.xpath('//somenode@someattr'): attr.text = 'sometext' But since the xpath method returns strings for the attributes this is not possible. Is there some way to make xpath return a list of node objects instead? Greetings Florian
Florian Wagner wrote:
I want to be able to do something like this with lxml
for attr in doc.xpath('//somenode@someattr'): attr.text = 'sometext'
But since the xpath method returns strings for the attributes this is not possible.
Ah, true, I hadn't considered that. Interesting consequence of the ElementTree API, though I still consider it to be right to return strings.
Is there some way to make xpath return a list of node objects instead?
It might be nice to have some flag that always returned elements. Then again, using xpath you can simply do something like this to get all nodes with a particular attribute: doc.xpath("//node()[@someattr]") and this to get all somenode nodes with someattr: doc.xpath("//somenode[@someattr]") and this to get all somenode nodes with someattr being "Foo" doc.xpath("//somenode[@someattr='Foo']") xpath is quite neat that way, though I hope I didn't put in any bugs in the examples. :) Regards, Martijn
participants (2)
-
Florian Wagner
-
Martijn Faassen