On 30.05.06 22:10:06, Stefan Behnel wrote:
Andreas Pakulat wrote:
However if I want to highlight the tree node that the xpath matches I have a "problem" when the xpath matches attributes or text nodes. So the question is: Is there a way using lxml to find out to which element a certain non-element result of an xpath evaluation belongs?
Not straight away. Both are returned as strings, so you loose the information where it came from.
You can try to run a second XPath expression to find the result text or attribute value in the tree, but that's bound to fail if text data is not unique (which is pretty likely for attributes).
I tried a few things and to me it seems running a second XPath-Expression using the extra step /parent::node() gives me the element node.
Now the question is: Can I assume that the last step either contains text() or attribute::<attribute name> or @attrname?
The only problem I see is that I need to traverse the text-childs of the elements returned when the XPath selects text nodes to know which strings belong to which elements.
Are there other ways to get at text() or attribute nodes?