LoginSignup
4
4

More than 5 years have passed since last update.

Nokogiriでcase-insensitiveなselect

Last updated at Posted at 2012-09-15

またNokogiriです。
時々タグ内の情報がcase sensitiveな感じで引っかからないので、対策します。

sample
doc.xpath("//meta[translate(@name, 'DESCRIPTION', 'description') = 'description']")

例はmetaタグ内のdescriptionです。
@name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ''abcdefghijklmnopqrstuvwxyz''
としておけば全文字で大丈夫です。
ですけどこの対策って何かあんまりスマートではないですよね。
phpの情報ではXPath2.0のmatchesファンクションには引数に'i'を渡せばcase-insensitiveにやってくれるみたいなんですが、Nokogiriで試してみると
xmlXPathCompOpEval: function matches not found
となったので諦めました。

XPathFunctionsをcustomする方法もあるらしく、それがスマートな感じです。

sample_from_stackoverflow
class XpathFunctions

  def case_insensitive_equals(node_set, str_to_match)
    node_set.find_all {|node| node.to_s.downcase == str_to_match.to_s.downcase }
  end

end

page.parser.xpath("//meta[case_insensitive_equals(@name,'keywords')]", XpathFunctions.new)
で使います。

参考:
How can I create a nokogiri case insensitive Xpath selector?

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4