How find() works for a node returned by xpath()?
Hi, I am not able to find the 'rab' node using the following code. Could anybody help me understand why it is the case? $ cat ./main.py #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8: from lxml import etree import sys from StringIO import StringIO tree = etree.parse(StringIO('<foo><bar><rab>abc</rab></bar><mybar>xyz</mybar></foo>')) r = tree.xpath('/foo') print len(r) print r[0].find('bar').tag print r[0].find('rab') print r[0].find('mybar') print r[0].find('nobar') $ ./main.py 1 bar None <Element mybar at 0x1082416c8> None -- Regards, Peng
I recommend reading this section: http://lxml.de/tutorial.html#elementpath --Chris On Sun, Dec 17, 2017 at 3:53 PM, Peng Yu <pengyu.ut@gmail.com> wrote:
Hi,
I am not able to find the 'rab' node using the following code. Could anybody help me understand why it is the case?
$ cat ./main.py #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:
from lxml import etree import sys
from StringIO import StringIO tree = etree.parse(StringIO('<foo><bar><rab>abc</rab></bar>< mybar>xyz</mybar></foo>'))
r = tree.xpath('/foo') print len(r) print r[0].find('bar').tag print r[0].find('rab') print r[0].find('mybar') print r[0].find('nobar') $ ./main.py 1 bar None <Element mybar at 0x1082416c8> None
-- Regards, Peng _________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
OK. According to my test, it seems the following statements are equivalent. Is it? Thanks. etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>") etree.parse(StringIO("<root><a x='123'>aText<b/><c/><b/></a></root>")) On Sun, Dec 17, 2017 at 6:53 PM, Chris Jerdonek <chris.jerdonek@gmail.com> wrote:
I recommend reading this section: http://lxml.de/tutorial.html#elementpath
--Chris
On Sun, Dec 17, 2017 at 3:53 PM, Peng Yu <pengyu.ut@gmail.com> wrote:
Hi,
I am not able to find the 'rab' node using the following code. Could anybody help me understand why it is the case?
$ cat ./main.py #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:
from lxml import etree import sys
from StringIO import StringIO tree = etree.parse(StringIO('<foo><bar><rab>abc</rab></bar><mybar>xyz</mybar></foo>'))
r = tree.xpath('/foo') print len(r) print r[0].find('bar').tag print r[0].find('rab') print r[0].find('mybar') print r[0].find('nobar') $ ./main.py 1 bar None <Element mybar at 0x1082416c8> None
-- Regards, Peng _________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
-- Regards, Peng
Hi, Am Sun, 17 Dec 2017 20:25:01 -0600 schrieb Peng Yu <pengyu.ut@gmail.com>:
According to my test, it seems the following statements are equivalent. Is it? Thanks.
etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>")
etree.parse(StringIO("<root><a x='123'>aText<b/><c/><b/></a></root>"))
Not entirely. The first expression gives you a direct root element node whereas the second expression gives you an ElementTree:
root1 = etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>") type(root1) lxml.etree._Element
root2 = etree.parse(StringIO("<root><!-- ... --></root>")) type(root2) lxml.etree._ElementTree
-- Gruß/Regards, Thomas Schraitle
Hi, Am Sun, 17 Dec 2017 17:53:36 -0600 schrieb Peng Yu <pengyu.ut@gmail.com>:
I am not able to find the 'rab' node using the following code. Could anybody help me understand why it is the case?
[...] from StringIO import StringIO tree = etree.parse(StringIO('<foo><bar><rab>abc</rab></bar><mybar>xyz</mybar></foo>'))
r = tree.xpath('/foo') print len(r) print r[0].find('bar').tag print r[0].find('rab')
This is wrong. You try to find a child node "rab" from the "foo" node. However, "rab" is a direct child of "bar", not "foo". See below.
[...]
Your XML structure looks like this: / foo bar rab mybar The absolute XPath to your "rab" node is this: /foo/bar/rab You can reach it with the following statements which are all the same:
rab1 = tree.xpath('/foo/bar/rab')[0] rab2 = r[0].find('bar').find('rab') rab3 = r[0].find('bar/rab')
# Prove: print(rab1 == rab2 == rab3) print(rab1 is rab2 is rab3) Hope that helps. :) -- Gruß/Regards, Thomas Schraitle
participants (3)
-
Chris Jerdonek
-
Peng Yu
-
Thomas Schraitle