Find nodes defined in corrupted namespace
<p>I've downloaded <a href="http://udcdata.info/udcsummary-skos.zip" rel="nofollow">this</a> XML file.</p>

<p>I'm trying to get <code>includingNote</code> as follows:</p>

<pre><code>...
namespaces = { "skos" : "http://www.w3.org/2004/02/skos/core#", "xml" : "http://www.w3.org/XML/1998/namespace", 
                 "udc" : "http://udcdata.info/udc-schema#" }
...


includingNote = child.find("udc:includingNote[@xml:lang='en']", namespaces)
if includingNote:
  print includingNote.text.encode("utf8")
</code></pre>

<p>The scheme is located <a href="http://udcdata.info/udc-scheme" rel="nofollow">here</a> and seems to be corrupted.</p>

<p>Is there a way I can print <code>includingNote</code> for each child node.</p>

<p>It is true that the <code>skos</code> prefix is not declared in udc-scheme, but searching the XML document is not a problem. </p>

<p>The following program extracts 639 <code>includingNote</code> elements:</p>

<pre><code>from xml.etree import cElementTree as ET

namespaces = {"udc" : "http://udcdata.info/udc-schema#",
              "xml" : "http://www.w3.org/XML/1998/namespace"}

doc = ET.parse("udcsummary-skos.rdf")
includingNotes = doc.findall(".//udc:includingNote[@xml:lang='en']", namespaces)

print len(includingNotes)   # 639

for i in includingNotes:
    print i.text
</code></pre>

<p>Note the use of <code>findall()</code> and <code>.//</code> in front of the element name in order to search the whole document.</p>

<hr>

<p>Here is a variant that returns the same information by first finding all <code>Concept</code> elements:</p>

<pre><code>from xml.etree import cElementTree as ET

namespaces = {"udc" : "http://udcdata.info/udc-schema#",
              "skos" : "http://www.w3.org/2004/02/skos/core#",
              "xml" : "http://www.w3.org/XML/1998/namespace"}

doc = ET.parse("udcsummary-skos.rdf")
concepts = doc.findall(".//skos:Concept", namespaces)

for c in concepts:
    includingNote = c.find("udc:includingNote[@xml:lang='en']", namespaces)
    if includingNote is not None:
        print includingNote.text
</code></pre>

<p>Note the use of <code>is not None</code>. Without that, it does not work. This seems to be a peculiarity of ElementTree. See <a href="http://stackoverflow.com/q/20129996/407651">Why does bool(xml.etree.ElementTree.Element) evaluate to False?</a>.</p>

python xml xml-namespaces elementtree