views:

367

answers:

1

I'm using nokogiri to select the 'keywords' attribute like this:

puts page.parser.xpath("//meta[@name='keywords']").to_html

One of the pages I'm working with has the keywords label with a capital "K" which has motivated me to make the query case insensitive.

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

So, my question is: What is the best way to make a nokogiri selection case insensitive?

EDIT Tomalak's suggestion below works great for this specific problem. I'd like to also use this example to help understand nokogiri better though and have a couple issues that I'm wondering about and have not been successful searching for. For example, are the regex 'pseudo classes' Nokogiri Docs appropriate for a problem like this?

I'm also curious about the matches?() method in nokogiri. I have not been able to find any clarification on the method. Does it have anything to do with the 'matches' concept in XPath 2.0 (and therefore could it be used to solve this problem)?

Thanks very much.

+3  A: 

Wrapped for legibility:

puts page.parser.xpath("
  //meta[
    translate(
      @name, 
      'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
      'abcdefghijklmnopqrstuvwxyz'
    ) = 'keywords'
  ]
").to_html

There is no "to lower case" function in XPath 1.0, so you have to use translate() for this kind of thing. Add accented letters as necessary.

Tomalak
Thanks much Tomalak. This solution is working well for me.
Rick
FYI, VTD-XML's xpath 1.0 actually implements upperCase and lowerCase as some sort of intermediate step to 2.0
vtd-xml-author