title: Get following node in different ancestor using lxml and xpath
body: <p>I'm writing a text-to-speech program that reads math equations. I have a thread that needs to pull math equations (as MathJax SVG's) and parse them to prose.</p>
  
  <p>Because of how the content is laid out, the math equations can be arbitrarily nested in other elements, like paragraphs, bolds, tables, etc.</p>
  
  <p>Using a reference to the current element, how do I get the next <code>&lt;span class="MathJax_SVG"&gt;</code>, which may be embedded in some other parent/ancestor?</p>
  
  <p>I tried to solve it using the following:</p>
  
  <pre><code>nextMath = currentElement.xpath('following::.//span[@class=\'MathJax_SVG\']')
  </code></pre>
  
  <p>Returns nothing, even though I can confirm visually that there is something following it. I tried removing the period, but <code>lxml</code> complains that my XPath is malformed.</p>
  
  <p>Have you guys ran into this before?</p>
  
  <p>P.S. Here is a test document to show my point:</p>
  
  <pre><code>&lt;html&gt;
     &lt;head&gt;
        &lt;title&gt;Test Document&lt;/title&gt;
     &lt;/head&gt;
     &lt;body&gt;
        &lt;h1 id="mainHeading"&gt;The Quadratic Formula&lt;/h1&gt;
        &lt;p&gt;The quadratic formula is used to solve quadratic equations. Here is the formula:&lt;/p&gt;
        &lt;p&gt;&lt;span class="MathJax_SVG" id="MathJax_Element_Frame_1"&gt;removed the SVG&lt;/span&gt;&lt;/p&gt;
        &lt;p&gt;Here are some possible values when you use the formula:&lt;/p&gt;
        &lt;p&gt;
        &lt;table&gt;
           &lt;tr&gt;
              &lt;td&gt;&lt;span class="MathJax_SVG" id="MathJax_Element_Frame_2"&gt;removed the SVG&lt;/span&gt;&lt;/td&gt;
              &lt;td&gt;&lt;span class="MathJax_SVG" id="MathJax_Element_Frame_3"&gt;removed the SVG&lt;/span&gt;&lt;/td&gt;
           &lt;/tr&gt;
           &lt;tr&gt;
              &lt;td&gt;&lt;span class="MathJax_SVG" id="MathJax_Element_Frame_4"&gt;removed the SVG&lt;/span&gt;&lt;/td&gt;
              &lt;td&gt;&lt;span class="MathJax_SVG" id="MathJax_Element_Frame_5"&gt;removed the SVG&lt;/span&gt;&lt;/td&gt;
           &lt;/tr&gt;
        &lt;/table&gt;
        &lt;/p&gt;
     &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Updates</strong></p>
  
  <p>Learned that <code>lxml</code> doesn't support absolute positions. This may be relevant.</p>
  
  <p><strong>Some Testing Code (assuming you saved HTML as test.html)</strong></p>
  
  <pre><code>from lxml import html
  
  # Get my html element
  with open('test.html', 'r') as f:
      myHtml = html.fromstring(f.read())
  
  # Get the first MathJax element
  start = myHtml.find('.//h1[@id=\'mainHeading\']')
  
  print 'My start:', html.tostring(start)
  
  # Get next math equation
  nextXPath = 'following::.//span[@class=\'MathJax_SVG\']'
  nextElem = start.xpath(nextXPath)
  
  if len(nextElem) &gt; 0:
      print 'Next equation:', html.tostring(nextElem[0])
  else:
      print 'No next equation...'
  </code></pre>