[lxml-dev] Fwd: Re: XPath exceptions

jholg at gmx.de jholg at gmx.de
Wed May 16 08:56:38 CEST 2007


Hi,

a quick question on nsmaps in XPath:
Is it intentional that this

>>>
>>> root = etree.fromstring("<root><a>what?</a></root>")
>>> root.xpath("//a", {'':'my/foo/bar/URI'})
['what?']
>>> root.xpath("//a")
['what?']
>>>

but 

>>> root = etree.fromstring("<root xmlns='my/foo/bar/URI'><a>what?</a></root>")
>>> root.xpath("//a", {'':'my/foo/bar/URI'})
[]

does not?
Of course you should do:

>>> root.xpath("//foo:a", {'foo':'my/foo/bar/URI'})
['what?']
>>>

Seems like an empty string prefix in an XPath-nsmap does not have the
desired effect, basically it is ignored as ns-prefix.
I notice that None is explicitly disallowed in an XPath-nsmap argument:

>>> root.xpath("//a", {None:'my/foo/bar/URI'})
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "etree.pyx", line 1042, in etree._Element.xpath
  File "xpath.pxi", line 222, in etree.XPathElementEvaluator.__init__
  File "xpath.pxi", line 102, in etree._XPathEvaluatorBase.__init__
  File "xpath.pxi", line 54, in etree._XPathContext.__init__
  File "extensions.pxi", line 73, in etree._BaseContext.__init__
TypeError: empty namespace prefix is not supported in XPath
>>>

Maybe '' should internally get converted to None and thus raise the same
error when used in an xpath nsmap argument dictionary?

Holger
-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger

-- 
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail


More information about the lxml-dev mailing list