
Hi, I am trying to parse the following xml file with xmlns. But I got the following error. Could anybody show me what is the correct way to call xpath()? ==> main.py <== #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8: from lxml import etree import sys tree = etree.parse(sys.stdin) print tree.xpath('tr', namespaces={None: "http://www.w3.org/TR/html4/"}) ==> main.xml <== <table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> $ ./main.py < main.xml Traceback (most recent call last): File "./main.py", line 7, in <module> print tree.xpath('tr', namespaces={None: "http://www.w3.org/TR/html4/"}) File "src/lxml/lxml.etree.pyx", line 2269, in lxml.etree._ElementTree.xpath (src/lxml/lxml.etree.c:66309) File "src/lxml/xpath.pxi", line 327, in lxml.etree.XPathDocumentEvaluator.__init__ (src/lxml/lxml.etree.c:167199) File "src/lxml/xpath.pxi", line 261, in lxml.etree.XPathElementEvaluator.__init__ (src/lxml/lxml.etree.c:166233) File "src/lxml/xpath.pxi", line 133, in lxml.etree._XPathEvaluatorBase.__init__ (src/lxml/lxml.etree.c:164467) File "src/lxml/xpath.pxi", line 57, in lxml.etree._XPathContext.__init__ (src/lxml/lxml.etree.c:163400) File "src/lxml/extensions.pxi", line 84, in lxml.etree._BaseContext.__init__ (src/lxml/lxml.etree.c:152398) TypeError: empty namespace prefix is not supported in XPath -- Regards, Peng

adding a namespace and then using in the selector works: from lxml import etree import sys tree = etree.parse(sys.stdin) print tree.xpath('ns:tr', namespaces={"ns": "http://www.w3.org/TR/html4/"}) On 09/25/2018 11:05 PM, Peng Yu wrote:
-- Brad Clements, bkc@murkworks.com (315)268-1000 Jabber/XMPP: bkclements@gmail.com

adding a namespace and then using in the selector works: from lxml import etree import sys tree = etree.parse(sys.stdin) print tree.xpath('ns:tr', namespaces={"ns": "http://www.w3.org/TR/html4/"}) On 09/25/2018 11:05 PM, Peng Yu wrote:
-- Brad Clements, bkc@murkworks.com (315)268-1000 Jabber/XMPP: bkclements@gmail.com
participants (2)
-
Brad Clements
-
Peng Yu