[lxml-dev] xpath on newly created elements

Doug Winter doug at isotoma.com
Sat Jul 7 14:45:35 CEST 2007


I can't make xpath work on elements that have been created using 
etree.Element when they have a namespace that doesn't use Clark notation.

I have a test case:

-- begins --

from lxml import etree

print "lxml.etree:       ", etree.LXML_VERSION
print "libxml used:      ", etree.LIBXML_VERSION
print "libxml compiled:  ", etree.LIBXML_COMPILED_VERSION
print "libxslt used:     ", etree.LIBXSLT_VERSION
print "libxslt compiled: ", etree.LIBXSLT_COMPILED_VERSION

nsmap=dict(test="http://test.com")

e = []

e.append(etree.fromstring('<test:foo xmlns:test="http://test.com" />'))
e.append(etree.Element("test:foo", nsmap=nsmap))
e.append(etree.Element("test:foo", {'xmlns:test': nsmap['test']}))
e.append(etree.Element("{%(test)s}foo" % nsmap))
e.append(etree.Element("{%(test)s}foo" % nsmap, nsmap=nsmap))

for i, elem in enumerate(e):
     print i, elem.xpath("/test:foo", nsmap)

-- ends --

I get this output if I run the above:

lxml.etree:        (1, 3, 2, 0)
libxml used:       (2, 6, 27)
libxml compiled:   (2, 6, 27)
libxslt used:      (1, 1, 20)
libxslt compiled:  (1, 1, 20)
0 [<Element {http://test.com}foo at b7a18374>]
1 []
2 []
3 [<Element {http://test.com}foo at b7a1848c>]
4 [<Element {http://test.com}foo at b7a184dc>]

I would expect all 5 cases to match the root element, but cases 1 and 2 
do not.

It appears to be only for elements created using namespace prefixes - 
and yet these work perfectly well in all other respects.

Is this a bug, or should elements not be created this way?

Cheers,

Doug.

-- 
Isotoma, Open Source Software Consulting - http://www.isotoma.com
Tel: 01904 567349, Mobile: 07879 423002, Fax: 020 79006980
Postal Address: Tower House, Fishergate, York, YO10 4UA, UK

Registered in England.  Company No 5171172.  VAT GB843570325.
Registered Office: 19a Goodge Street, London, W1T 2PH


More information about the lxml-dev mailing list