logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Re: Type returned by xpath: msg#00002

Subject: Re: Type returned by xpath
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


<Prev in Thread] Current Thread [Next in Thread>