I am trying to map a country or country using the lower-case function in XPath. translate is useless, so using lower case, and my version of Python 2.6.6 has XPath 2.0 support, I suppose since lower case is only available in XPath 2.0.
How can I put lower case for use in my case, this is what I am looking for. Hope the example in itself. I am looking for ['USA', 'US'] as a result (both countries in one move, which can happen if lower case evaluates the country and the country the same).
HTML: doc.htm
<html> <table> <tr> <td> Name of the Country : <span> USA </span> </td> </tr> <tr> <td> Name of the country : <span> UK </span> </td> </tr> </table>
Python:
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()')
Update:
out.xpath('//table/tr/td[text()[contains(translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz") , "country")]]/span/text()')
Now the question remains: can I save part of the translation as a global variable βhandlecaseβ and print this global variable whenever I do XPath?
Something like this works:
handlecase = """translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")""" out.xpath('//table/tr/td[text()[contains(%s , "country")]]/span/text()' % (handlecase))
But for simplicity and readability, I want to run it as follows:
out.xpath('//table/tr/td[text()[contains(handlecase , "country")]]/span/text()')