python - lxml - Default name spaces -


i trying parse xml file using lxml.

my_tree = etree.parse(file) my_root = my_tree.getroot()  child in my_root:     print(child.tag)  # {some default namespace}prop # {some default namespace}prop # {some default namespace}stuff # ... 

ideally, want elements want like

my_root.findall('prop', my_root.nsmap) 

but returning empty list. noticed my_root.nsmap dictionary had none item default namespace.

nsmap = {none: 'default namespace', ...} 

i found quick workaround copying nsmap , adding 'default' item same value none item, , do

my_root.findall('default:prop', new_map) 

this feels hackish. why none in namespace map? there straightforward method in lxml automatically uses default namespace?

edit: xml looking @ along lines of

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <scenarioprops xmlns="http://filler.com/default.xsd" xmlns:ns2="http://filler.com/ns.xsd" id="test">     <prop id="wi-fi">         <ns2:position x="0.0" y="0.0" z="0.0"/>         <ns2:orientation roll="0.0" pitch="0.0" yaw="0.0"/>     </prop> </scenarioprops> 

hackish or not, have specify prefix. xpath 1.0, lxml supports, not have concept of default namespace (it works differently in xpath 2.0, not apply here).

the other option not bother prefixes @ all. use qualified element name in "clark notation" instead:

 my_root.findall('{http://filler.com/default.xsd}prop'). 

see http://lxml.de/faq.html#how-can-i-specify-a-default-namespace-for-xpath-expressions.


Comments