Nokogiri allows custom XPath functions. The nokogiri docs that you link to show an inline class definition for when you're only using it once. If you have a lot of custom functions or if you use the case-insensitive match a lot, you may want to define it in a class.
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
Then call it like any other XPath function, passing in an instance of your class as the 2nd argument.
page.parser.xpath("//meta[case_insensitive_equals(@name,'keywords')]",
XpathFunctions.new).to_html
In your Ruby method, node_set
will be bound to a Nokogiri::XML::NodeSet
. In the case where you're passing in an attribute value like @name
, it will be a NodeSet with a single Nokogiri::XML::Attr
. So calling to_s
on it gives you its value. (Alternatively, you could use node.value
.)
Unlike using XPath translate
where you have to specify every character, this works on all the characters and character encodings that Ruby works on.
Also, if you're interested in doing other things besides case-insensitive matching that XPath 1.0 doesn't support, it's just Ruby at this point. So this is a good starting point.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…