Hi Andreas, Andreas Pakulat wrote:
On 03.06.06 12:59:10, Stefan Behnel wrote:
Andreas Pakulat wrote:
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?
I mean if the result of an XPath is a list of strings, can I assume that this was created by either a ::text() or an attribute:: expression.
What about "string(a)" ?
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...
Hmm, could you give me an example for something like that? I'm not that familiar with XPath...
from lxml import etree el = etree.Element("root") etree.SubElement(el, "a") <Element a at 2b017c7aefc8> etree.SubElement(el, "b") <Element b at 2b017c7aef38> el.xpath("a|b|c|d") [<Element a at 2b017c7aef38>, <Element b at 2b017c7b1050>] el.xpath("string(a|b|c|d)") '' el.xpath("string(a|b|c|d)|string(a)") ''
There's all sorts of weird expressions you could come up with...
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.
Ah, I didn't see "|" until now. Well, that makes the whole thing a bit "harder", because I can't tell wether a given string is created from a text node or is the value of an attribute.
It already makes it harder to find its parent element. You may still end up having to parse the expression to find partial '|' expressions etc.
Are there other ways to get at text() or attribute nodes? What do you mean?
I mean, is there another way to have the text node of an element as result of the xpath expression, other than having the text() function somewhere (probably at the end of the path)? The same for attributes, is there another way to get the values of attributes in the result, other than using attribuge::(*|<name>) or using @<name>?
Functions are a good way. Stefan