Hi Andreas, Andreas Pakulat wrote:
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.
Sure, good idea.
Now the question is: Can I assume that the last step either contains text() or attribute::<attribute name> or @attrname?
You mean as the result of an XPath expression? Well, you may get back bool values or generated strings (can you?), in which case you can't expect to find out what node (or nodes) they came from. Also, AFAIR, you can merge multiple XPath expressions into one and that case may be hard to detect. The last part of an XPath expression is not always what returned the result...
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.
Note that you can get back a wild combination of strings, nodes and numbers, so there is a bit of work to do anyway.
Are there other ways to get at text() or attribute nodes?
What do you mean? Stefan