module Nokogiri::CSS
def xpath_for(
Nokogiri::CSS.xpath_for("h1, h2, h3") # => ["//h1", "//h2", "//h3"]
*Example* with a selector list:
Nokogiri::CSS.xpath_for("h1 + div") # => ["//h1/following-sibling::*[1]/self::div"]
*Example* with a complex selector:
Nokogiri::CSS.xpath_for("div.xl") # => ["//div[contains(concat(' ',normalize-space(@class),' '),' xl ')]"]
*Example* with a compound selector:
Nokogiri::CSS.xpath_for("div") # => ["//div"]
*Example* with a simple selector:
[Returns] (Array
don't incur the overhead of re-parsing the selector. Default is +true+.
Whether to use the SelectorCache for the translated query to ensure that repeated queries
- +cache:+ (Boolean)
provided, +prefix+ and +ns+ must not be present.
⚠ Note that this option is mutually exclusive with +prefix+ and +ns+. If +visitor+ is
be customized for your document type. Default is +Nokogiri::CSS::XPathVisitor.new+.
Nokogiri::CSS::XPathVisitor for more information on some of the complex behavior that can
Use this XPathVisitor object to transform the CSS AST into XPath expressions. See
- +visitor:+ (Nokogiri::CSS::XPathVisitor)
empty set of namespaces.
namespace prefix and the values are the namespace URIs. Default is +nil+ indicating an
Namespaces that are referenced in the query, if any. This is a hash where the keys are the
- +ns:+ (Hash
for standard options. Default is +XPath::GLOBAL_SEARCH_PREFIX+.
The XPath expression prefix which determines the search context. See Nokogiri::XML::XPath
- +prefix:+ (String)
[Keyword arguments]
examples).
value may be a {selector list}[https://www.w3.org/TR/selectors-4/#grouping] (see
The CSS selector to be translated into XPath. This is always a String, but that string
- +selector_list+ (String)
[Parameters]
functions.
some extended syntax that Nokogiri supports, and advanced CSS features like pseudo-class
Also see Nokogiri::XML::Searchable#css for documentation on supported CSS selector features,
\CSS selectors to XPath directly.
mechanism used by XML::Searchable and is provided solely for advanced users to translate
node classes, for querying documents with CSS selectors. This method is the underlying
⚠ Users should prefer Nokogiri::XML::Searchable#css, which is mixed into all document and
💡 Note that translated queries are cached by default for performance concerns.
Translate a CSS selector list to the equivalent XPath expressions.
xpath_for(selector_list [, prefix:] [, ns:] [, visitor:] [, cache:]) → Array
xpath_for(selector_list) → Array
:call-seq:
def xpath_for( selector, options = nil, prefix: options&.delete(:prefix), visitor: options&.delete(:visitor), ns: options&.delete(:ns), cache: true ) unless options.nil? warn("Nokogiri::CSS.xpath_for: Passing options as an explicit hash is deprecated. Use keyword arguments instead. This will become an error in a future release.", uplevel: 1, category: :deprecated) end raise(TypeError, "no implicit conversion of #{selector.inspect} to String") unless selector.respond_to?(:to_str) selector = selector.to_str raise(Nokogiri::CSS::SyntaxError, "empty CSS selector") if selector.empty? if visitor raise ArgumentError, "cannot provide both :prefix and :visitor" if prefix raise ArgumentError, "cannot provide both :ns and :visitor" if ns end visitor ||= begin visitor_kw = {} visitor_kw[:prefix] = prefix if prefix visitor_kw[:namespaces] = ns if ns Nokogiri::CSS::XPathVisitor.new(**visitor_kw) end if cache key = SelectorCache.key(selector: selector, visitor: visitor) SelectorCache[key] ||= Parser.new.xpath_for(selector, visitor) else Parser.new.xpath_for(selector, visitor) end end