Davinj
Davinj

Reputation: 347

How can I create a nokogiri case insensitive text * search?

Currnetly I am doing

words = []
words << "philip morris"
words << "Philip morris"
words << "philip Morris"
words << "Philip Morris"
for word in words
  doc.search("[text()*='#{word}']")
end

When I was using hpricot I found where to downcase the results within the gem so I could just keep all my searchs lowercase, however nokogiri has been quite difficult to find where one could even do that. Is anyone aware of a way to do this? Thank you very much for your time

Upvotes: 3

Views: 1443

Answers (1)

mikej
mikej

Reputation: 66263

The lower-case XPath function is not available but you can use the translate XPath 1.0 function to convert your text to lowercase e.g. for the English alphabet:

translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')

I couldn't seem to use this in combination with the *= operator but you can use contains to do a substring search instead, making the full thing:

doc.search("//*[contains(translate(text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'philip morris')]")

Upvotes: 3

Related Questions