I'm trying to parse XML document to return <input> nodes that contain a ref attribute. A toy example works but the document itself returns an empty array, when it should show a match.
toy example
import elementtree.ElementTree
from lxml import etree
tree = etree.XML('<body><input ref="blabla"><label>Cats</label></input><input ref="blabla"><label>Dogs</label></input><input ref="blabla"><label>Birds</label></input></body>')
# I can return the relevant input nodes with:
print len(tree.findall(".//input[@ref]"))
2
But working with the following (reduced) file for some reason fails:
example.xml
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>A title</h:title>
</h:head>
<h:body>
<group ref="blabla">
<label>Group 1</label>
<input ref="blabla">
<label>Field 1</label>
</input>
</group>
</h:body>
</h:html>
script
import elementtree.ElementTree
from lxml import etree
with open ("example.xml", "r") as myfile:
xml = myfile.read()
tree = etree.XML(xml)
print len(tree.findall(".//input[@ref]"))
0
Any idea why this fails, and how to workaround? I think it may have something to do with the XML header. Very grateful for any assistance.