Search code examples
pythonstringxpathcase-insensitive

How to make string matching case insensitiv [text()="sTrInG"] in python (lxml)?


I have this xpath pattern:

tags = doc.xpath('/html/body//a[text() = "' + name.encode('utf8') + '"]/@href'

This returns the url of every -tag with name name as text in it. Is it possible to get a case insensitiv matching of name?

Edit

When trying @Shelhamer solution I get:

>>> a_tag_list = html_string.xpath('/html/body//a[lower-case(text()) = "' + author_name.lower() + '"]/@href')
  File "lxml.etree.pyx", line 1459, in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:40530)
  File "xpath.pxi", line 324, in lxml.etree.XPathElementEvaluator.__call__ (src/lxml/lxml.etree.c:113864)
  File "xpath.pxi", line 242, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:113063)
  File "xpath.pxi", line 227, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:112894)
lxml.etree.XPathEvalError: Unregistered function

Solution

  • This can be done by using the lower-case function:

    tags = doc.xpath('/html/body//a[lower-case(text()) = "' + name.encode('utf8') + '"]/@href'
    

    A helpful list of functions is here: http://www.w3schools.com/xpath/xpath_functions.asp