title: Python: adding namespaces in lxml
body: <p>I'm trying to specify a namespace using <em>lxml</em> similar to this example (taken from <a href="http://www.maconstateit.net/tutorials/XML/XML03/xml03-01.htm" rel="nofollow noreferrer">here</a>):</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;TreeInventory xsi:noNamespaceSchemaLocation="Trees.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;/TreeInventory&gt;
  </code></pre>
  
  <p>I'm not sure how to add the Schema instance to use and also the Schema location.
  The <a href="http://codespeak.net/lxml/tutorial.html#namespaces" rel="nofollow noreferrer">documentation</a> got me started, by doing something like:</p>
  
  <pre class="lang-py prettyprint-override"><code>&gt;&gt;&gt; NS = 'http://www.w3.org/2001/XMLSchema-instance'
  &gt;&gt;&gt; TREE = '{%s}' % NS
  &gt;&gt;&gt; NSMAP = {None: NS}
  &gt;&gt;&gt; tree = etree.Element(TREE + 'TreeInventory', nsmap=NSMAP)
  &gt;&gt;&gt; etree.tostring(tree, pretty_print=True)
  '&lt;TreeInventory xmlns="http://www.w3.org/2001/XMLSchema-instance"/&gt;\n'
  </code></pre>
  
  <p>I'm not sure how to specify it an instance though, and then also specify a location.  It seems like this can be done with the <code>nsmap</code> keyword-arg in <code>etree.Element</code>, but I don't see how.</p>