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

Currnetly i do

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

When I used hpricot, I found where it was possible to reduce the results in stone, so that I could just keep all my searches in lower case, however nokogiri was quite difficult to find where to do this. Does anyone know how to do this? Thank you so much for your time.

+3
source share
1 answer

The lower-caseXPath function is not available, but you can use the translateXPath 1.0 function to convert text to lowercase, for example. for the English alphabet:

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

*=, contains , :

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

Source: https://habr.com/ru/post/1766729/


All Articles