title: LXML Xpath Query
body: <p>I'm in the middle of writing a small and dirty module to convert an XML document to JSON so that various Javascript libraries can display it in a table. This involves me learning to use LXML and its various XPath functions.</p>
  <p>I have the following block of code:</p>
  <pre class="lang-py prettyprint-override"><code>    def parse(self):
  
          parser = etree.XMLParser(remove_comments=True, encoding=&quot;UTF-8&quot;, no_network=True, recover=True)
          root = etree.XML(self.text, parser=parser)
          self.tree = etree.XPathElementEvaluator(root)
  
          print(f&quot;test: { self.tree('/*') }&quot;)
  </code></pre>
  <p>In my unit test, this outputs the following:</p>
  <pre><code>test_parse (test_converter.TestConverter) ... test: [&lt;Element {http://www.ivoa.net/xml/VOTable/v1.3}VOTABLE at 0x7fa2b99a8dc0&gt;]
  </code></pre>
  <p>However, when I attempt to query as below, I get an empty list as the result:</p>
  <pre class="lang-py prettyprint-override"><code>print(f&quot;test: { self.tree('/VOTABLE*') }&quot;)
  </code></pre>
  <p>I've tried prepending the namespace to VOTABLE as below with no results either:</p>
  <pre class="lang-py prettyprint-override"><code>print(f&quot;test: { self.tree('/{http://www.ivoa.net/xml/VOTable/v1.3}VOTABLE*') }&quot;)
  </code></pre>
  <p>Can anyone tell me what rookie mistake I am making?</p>
  <p>Sample data:</p>
  <pre><code>&lt;VOTABLE version=&quot;1.4&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
    xmlns=&quot;http://www.ivoa.net/xml/VOTable/v1.3&quot;
    xsi:schemaLocation=&quot;http://www.ivoa.net/xml/VOTable/v1.3 http://www.ivoa.net/xml/VOTable/v1.3&quot;&gt;
   &lt;DESCRIPTION&gt;
     VizieR Astronomical Server vizier.u-strasbg.fr
      Date: 2020-11-07T11:43:26 [V1.99+ (14-Oct-2013)]
     Explanations and Statistics of UCDs:         See LINK below
     In case of problem, please report to:    cds-question@unistra.fr
     In this version, NULL integer columns are written as an empty string
     &amp;lt;TD&amp;gt;&amp;lt;/TD&amp;gt;, explicitely possible from VOTable-1.3
   &lt;/DESCRIPTION&gt;
   &lt;RESOURCE ID=&quot;yCat_3135&quot; name=&quot;III/135A&quot;&gt;
    ...
   &lt;/RESOURCE&gt;
   ...
  &lt;/VOTABLE&gt;
  </code></pre>
  <p><strong>UPDATE: SOLUTION</strong></p>
  <p>Once drec4s pointed out that I wasn't registering the namespace for the query, I managed to work out what I was doing wrong. Here is the working block of code:</p>
  <pre class="lang-py prettyprint-override"><code>        parser = etree.XMLParser(remove_comments=True, encoding=&quot;UTF-8&quot;, no_network=True, recover=True)
          root = etree.XML(self.text, parser=parser)
          self.tree = etree.XPathElementEvaluator(root)
          self.tree.register_namespace(&quot;n&quot;, &quot;http://www.ivoa.net/xml/VOTable/v1.3&quot;)
          test = self.tree(&quot;/n:VOTABLE/n:DESCRIPTION/text()&quot;)
  </code></pre>