Same xpath returns different values on Centos, Ubuntu
<p>We're trying to port our application from a Centos environment into other linuxes, in particular Ubuntu. The application is python-based so there should be no problems, however we noticed an odd behaviour when parsing XPATHs.</p>

<p>Sample file:</p>

<pre><code>&lt;root&gt;
    &lt;outer&gt;
        &lt;inner&gt;
            &lt;el/&gt;
        &lt;/inner&gt;
        &lt;inner&gt;
            &lt;el/&gt;
        &lt;/inner&gt;
    &lt;/outer&gt;
&lt;/root&gt;
</code></pre>

<p>Code:</p>

<pre><code>from lxml import etree
r = etree.parse('foo.xml')
print 'One: ', r.xpath('.//el[2]')
print 'Two: ', r.xpath('(.//el)[2]')
</code></pre>

<p>On Centos (python 2.7.5):</p>

<pre><code>One:  [&lt;Element el at 0x25fdcf8&gt;]
New version:  [&lt;Element el at 0x25fdcf8&gt;]
</code></pre>

<p>On RedHat Enterprise 6.6 (python 2.7.11)</p>

<pre><code>One:  []
Two:  [&lt;Element el at 0x7fa27198cd40&gt;]
</code></pre>

<p>On Debian and Ubuntu (python 2.7.9 and 2.7.12):</p>

<pre><code>One:  []
Two:  [&lt;Element el at 0x7f94ed6cf200&gt;]
</code></pre>

<p>This is the <strong>same</strong> code trying to access the second element <code>el</code>.</p>

<p>Debian, Ubuntu and Cents environment have lxml (installed from <code>pip</code>) version 3.6.4. When installing lxml it compiles it against libxml2 which is </p>

<p>On debian: </p>

<pre><code>    $ dpkg -l | grep libxml2-dev
ii  libxml2-dev:amd64                     2.9.1+dfsg1-5+deb8u2                 amd64
</code></pre>

<p>On centos:</p>

<pre><code>$ rpm -q libxml2-devel
libxml2-devel-2.9.1-6.el7_2.3.x86_64
</code></pre>

<p>RedHat runs etree 3.6.0:</p>

<pre><code>$ rpm -q libxml2-devel
libxml2-devel-2.7.6-21.el6.x86_64
</code></pre>

<p>What's going on here?</p>

<p>The result on CentOS is incorrect.</p>

<p><code>libxml2</code> 2.9.0 introduced a regression, see <a href="https://bugzilla.gnome.org/show_bug.cgi?id=695699" rel="nofollow">libxml 2.9.0 XPath evaluation issue</a>. It is fixed in 2.9.2 but not in 2.9.1.</p>

<p>Debian has integrated the patch in version 2.9.1+dfsg1-3:</p>

<blockquote>
  <p>libxml2 (2.9.1+dfsg1-3) unstable; urgency=low</p>
  
  <ul>
  <li>debian/patches/0007-Fix-XPath-optimization-with-predicates.patch:
  
  <ul>
  <li>Upstream patch to fix XPath evaluation issue. (Closes: #713146)</li>
  </ul></li>
  </ul>
</blockquote>

python linux xpath centos lxml