On 12/09/2021 23:31, Stefan Behnel wrote:
Am 12. September 2021 19:14:40 MESZ schrieb Gilles:
tracks = root.findall('.//snipet|.//LookAt|.//Style|.//StyleMap')
Just use
root.iter('snippet', 'LookAt', 'Style', 'StyleMap')
It's certainly faster, and I also find it nicer to read.
Much better. Thanks!
On 13/09/2021 00:21, Terry Brown wrote:
To answer your question (1):
//foo "recursively" searches the whole document from the root for foo
elements regardless of the element on which .xpath() is invoked.
.//foo "recursively" searches only the descendents of the element on
which .xpath() was invoked.
I don't think W3 schools explains that well, I usually prefer refs.
like https://developer.mozilla.org/en-US/docs/Web/XPath
<https://developer.mozilla.org/en-US/docs/Web/XPath> or
https://www.w3.org/TR/xpath-31/ <https://www.w3.org/TR/xpath-31/>
Here's some code that illustrates:
from lxml import etree
xml = """
<root>
<node id="a">
<value>One</value>
<node id="b">
<value>Two</value>
</node>
</node>
<node id="c">
</node>
</root>"""
dom = etree.fromstring(xml)
for node in dom.xpath("//node"):
print(node.get("id"))
print(" //", node.xpath("//value/text()"))
print(".//", node.xpath(".//value/text()"))
Thanks! As a newbie, ithe different ways of finding elements is a bit
confusing.
_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com