Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Look at the effbot namespaces documentation/examples; specifically the <a href="http://effbot.org/zone/element-namespaces.htm" rel="noreferrer">parse_map</a> function. It shows you how to add an *ns_map* attribute to each element which contains the prefix/URI mapping that applies to that specific element.</p> <p>However, that adds the ns_map attribute to all the elements. For my needs, I found I wanted a global map of all the namespaces used to make element look up easier and not hardcoded.</p> <p>Here's what I came up with:</p> <pre><code>import elementtree.ElementTree as ET def parse_and_get_ns(file): events = "start", "start-ns" root = None ns = {} for event, elem in ET.iterparse(file, events): if event == "start-ns": if elem[0] in ns and ns[elem[0]] != elem[1]: # NOTE: It is perfectly valid to have the same prefix refer # to different URI namespaces in different parts of the # document. This exception serves as a reminder that this # solution is not robust. Use at your own peril. raise KeyError("Duplicate prefix with different URI found.") ns[elem[0]] = "{%s}" % elem[1] elif event == "start": if root is None: root = elem return ET.ElementTree(root), ns </code></pre> <p>With this you can parse an xml file and obtain a dict with the namespace mappings. So, if you have an xml file like the following ("my.xml"):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/"\ &gt; &lt;feed&gt; &lt;item&gt; &lt;title&gt;Foo&lt;/title&gt; &lt;dc:creator&gt;Joe McGroin&lt;/dc:creator&gt; &lt;description&gt;etc...&lt;/description&gt; &lt;/item&gt; &lt;/feed&gt; &lt;/rss&gt; </code></pre> <p>You will be able to use the xml namepaces and get info for elements like <em>dc:creator</em>:</p> <pre><code>&gt;&gt;&gt; tree, ns = parse_and_get_ns("my.xml") &gt;&gt;&gt; ns {u'content': '{http://purl.org/rss/1.0/modules/content/}', u'dc': '{http://purl.org/dc/elements/1.1/}'} &gt;&gt;&gt; item = tree.find("/feed/item") &gt;&gt;&gt; item.findtext(ns['dc']+"creator") 'Joe McGroin' </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload