module Nokogiri::CSS

Translate a CSS selector into an XPath 1.0 query

Public Class Methods

xpath_for(selector) → String Show source
xpath_for(selector [, prefix:] [, visitor:] [, ns:]) → String
# File lib/nokogiri/css.rb, line 42
def xpath_for(selector, options = {})
  prefix = options.fetch(:prefix, Nokogiri::XML::XPath::GLOBAL_SEARCH_PREFIX)
  visitor = options.fetch(:visitor) { Nokogiri::CSS::XPathVisitor.new }
  ns = options.fetch(:ns, {})
  Parser.new(ns).xpath_for(selector, prefix, visitor)

Translate a CSS selector to the equivalent XPath query.

  • selector (String) The CSS selector to be translated into XPath

  • prefix: (String)

    The XPath prefix for the query, see Nokogiri::XML::XPath for some options. Default is XML::XPath::GLOBAL_SEARCH_PREFIX.

  • visitor: (Nokogiri::CSS::XPathVisitor)

    The visitor class to use to transform the AST into XPath. Default is Nokogiri::CSS::XPathVisitor.new.

  • ns: (Hash<String ⇒ String>)

    The namespaces that are referenced in the query, if any. This is a hash where the keys are the namespace prefix and the values are the namespace URIs. Default is an empty Hash.


(String) The equivalent XPath query for selector

💡 Note that translated queries are cached for performance concerns.

© 2008–2018 Aaron Patterson, Mike Dalessio, Charles Nutter, Sergio Arbeo,
Patrick Mahoney, Yoko Harada, Akinori MUSHA, John Shahid, Lars Kanis
Licensed under the MIT License.