Andreas Pakulat wrote:
very recently I needed a program that evaluates a xpath and displays the "result" graphically in a tree of the xml file that was used. I found XPath Explorer a java application, however it does quite some more stuff than I need and there certain things that just don't work as I want.
I figured I could very easily provide something similar using lxml and PyQt4.
That sounds pretty interesting. Please post a link when you have something usable.
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).
A 'stupid idea' would be to fiddle with the XPath expression and add a function call after each traversed node that stores the element in a list. You could then trace the evaluation path.
Something like "a/b//c[true()]/d/text()" -> "a[store(.)]/b[store(.)]//c[(true()) and store(.)]/d[store(.)]/text()"
But that would require you to 'parse' the expression, I don't know if you can get that done with regexps...