[lxml-dev] [Spam: 5.001 ] Re: Beginner question

Frederik Elwert felwert at uni-bremen.de
Thu Oct 11 16:26:45 CEST 2007


Am Donnerstag, den 11.10.2007, 14:35 +0200 schrieb Andreas Tille:
> On Thu, 11 Oct 2007, Stefan Behnel wrote:
> > But there *is* a namespace, so how would you distinguish it from a
plain
> > "source" tag without namespace?
> >
> > If it's just for brevity, you can always use string constants.
> 
> I decided for
> 
>      if elem.tag.endswith('}source'):
>          source = elem.get("idSource")
> 
> because for practical reasons I can be sure that I'm in the default
> name space.

If you want it a bit less "dirty" and more XMLish, you could use
local-name() from XPath:

lname = etree.XPath('local-name()')

if lname(elem) == 'source':
    source = elem.get('idSource')

Cheers,
Frederik



More information about the lxml-dev mailing list