Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a case insensitive value with XPath

Tags:

xpath

scrapy

I have an XPath with which I'm trying to match meta tags that have a name attribute with a value that contains the word 'keyword' irrespective of case. Basically, I'm trying to match:

<meta name="KEYWORDS">
<meta name="Keywords">
<meta name="keywords">

with the XPath

'descendant::meta[contains(lower-case(@name), "keyword")]/@content'

I'm using Scrapy and it's built-in Selectors, but when I try this XPath, I get an error "Invalid XPath:...". What am I doing wrong and what's the right way to do what I want?

like image 1000
elbear Avatar asked Jan 04 '11 19:01

elbear


People also ask

Which XPath function would you use to transform character to uppercase?

XPath/XQuery upper-case function.

What version of XPath does chrome use?

No, Chrome uses XPath 1.0.


1 Answers

Scrapy Selectors are built over the libxml2 library, which, AFAIK, doesn't support XPath 2.0. At least libxslt does not for sure.

You can use XPath 1.0 translate() to solve this. In general it will look like:

translate(yourString, 
          'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
          'abcdefghijklmnopqrstuvwxyz')
like image 163
Flack Avatar answered Dec 10 '22 12:12

Flack