[lxml-dev] iterparse and namespaces
Laurence Rowe
l at lrowe.co.uk
Tue Mar 17 21:47:34 CET 2009
Hi,
Use local-name() in an xpath:
>>> doc = etree.XML('''<x><a xmlns="http://foo"/><a xmlns="http://bar"/></x>''')
>>> doc.xpath("//*[local-name() = 'a']")
[<Element {http://foo}a at 1710270>, <Element {http://bar}a at 1710210>]
HTH,
Laurence
2009/3/17 Piotr Furman <piotr.furman at webservice.pl>:
> Hi,
>
> I've got small problem with iterparse and namespaces. I have this code:
>
>>>> from StringIO import StringIO
>>>> from lxml import etree
>>>> print etree.__version__
> 2.1.5
>>>> xml = """<root xmlns="http://www.example.com"><a>1</a><a>2</a></root>"""
>>>> a1 = etree.iterparse(StringIO(xml), tag="a")
>>>> a1.next()
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "iterparse.pxi", line 515, in lxml.etree.iterparse.__next__
> (src/lxml/lxml.etree.c:77014)
> StopIteration
>>>> a2 = etree.iterparse(StringIO(xml), tag="{http://www.example.com}a")
>>>> a2.next()
> (u'end', <Element {http://www.example.com}a at -488456c4>)
>
> Is that possible to get all tags "a" without passing namespace? I mean, a2
> works, but could it be possible to make a1 working too?
>
> regards
> Piotr Furman
>
> _______________________________________________
> lxml-dev mailing list
> lxml-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/lxml-dev
>
More information about the lxml-dev
mailing list