Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do a Python XPath case-insensitive search using lxml?
    primarykey
    data
    text
    <p>I am trying to match for country or Country using <code>lower-case</code> function in XPath. <code>translate</code> is kinda messy, so using lower-case and my Python version 2.6.6 has XPath 2.0 support I believe since lower-case is only available in XPath 2.0.</p> <p>How I can put lower-case to use in my case is what I am looking for. Hope the example is self explanatory. I am looking for <code>['USA', 'US']</code> as output (both countries in one go which can happen if lower-case evaluates Country and country to be the same).</p> <p><strong>HTML: doc.htm</strong></p> <pre><code>&lt;html&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; Name of the Country : &lt;span&gt; USA &lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Name of the country : &lt;span&gt; UK &lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p><strong>Python :</strong></p> <pre><code>import lxml.html as lh doc = open('doc.htm', 'r') out = lh.parse(doc) doc.close() print out.xpath('//table/tr/td[text()[contains(. , "Country")]]/span/text()') # Prints : [' USA '] print out.xpath('//table/tr/td[text()[contains(. , "country")]]/span/text()') # Prints : [' UK '] print out.xpath('//table/tr/td[lower-case(text())[contains(. , "country")]]/span/text()') # Prints : [&lt;Element td at 0x15db2710&gt;] </code></pre> <p><strong>Update :</strong> </p> <pre><code>out.xpath('//table/tr/td[text()[contains(translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") , "country")]]/span/text()') </code></pre> <p>Now the question remains, can I store the translate part as a global variable 'handlecase' and print that global variable whenever I do an XPath?</p> <p><strong>Something like this works :</strong></p> <pre><code>handlecase = """translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")""" out.xpath('//table/tr/td[text()[contains(%s , "country")]]/span/text()' % (handlecase)) </code></pre> <p>But for sake of simplicity and readability, I want to run it like this :</p> <pre><code>out.xpath('//table/tr/td[text()[contains(handlecase , "country")]]/span/text()') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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