i parsing xml has custom name space using lxml . excerpt xml given below.
<abcd:abcdcfg xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:abcd="http://www.xyzv.com/abcd"> <abcd:section name="features" display-name="features" desc=“parameters”> <abcd:param name=“mode”> <abcd:type>string</abcd:type> <abcd:persistent>true</abcd:persistent> <abcd:configurable>true</abcd:configurable> <abcd:readaccess>aup</abcd:readaccess> <abcd:writeaccess>ap</abcd:writeaccess> <abcd:displayname>mode</abcd:displayname> </abcd:param> </abcd:section> </abcd:abcdcfg>
right when finding values in xml using this
sections = xmltree.findall('{http://www.xyzv.com/abcd}section') if (child.tag =='{http://www.xyzv.com/abcd}param')
is there anyway in lxml enable me use lxml without namespace. like
sections = xmltree.findall('section') if (child.tag =='param')
this make code readable. welcome.
if applicable in case, can remove namespaces tree after parsing. i'd go this solution. working sample in python 3:
import lxml.etree et io import bytesio data = b"""<abcd:abcdcfg xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:abcd="http://www.xyzv.com/abcd"> <abcd:section name="features" display-name="features" desc="parameters"> <abcd:param name="mode"> <abcd:type>string</abcd:type> <abcd:persistent>true</abcd:persistent> <abcd:configurable>true</abcd:configurable> <abcd:readaccess>aup</abcd:readaccess> <abcd:writeaccess>ap</abcd:writeaccess> <abcd:displayname>mode</abcd:displayname> </abcd:param> </abcd:section> </abcd:abcdcfg>""" = et.iterparse(bytesio(data)) _, el in it: if '}' in el.tag: el.tag = el.tag.split('}', 1)[1] # strip namespaces root = it.root sections = root.findall('section') print(sections)
prints:
[<element section @ 0x10636d0c8>]
which means can find elements in tree without specifying namespaces @ all.