Html5rw.SelectorCSS selector engine.
This module provides CSS selector support for querying the DOM tree. CSS selectors are patterns used to select HTML elements based on their tag names, attributes, classes, IDs, and position in the document.
Example selectors:
div - all <div> elements#header - element with id="header".warning - elements with class="warning"div > p - <p> elements that are direct children of <div>[href] - elements with an href attributeCSS Selector Engine
This module provides CSS selector parsing and matching for querying the HTML5 DOM. It supports a subset of CSS3 selectors suitable for common web scraping and DOM manipulation tasks.
div, p, span#myid.myclass*[attr][attr="value"][attr~="value"][attr^="value"][attr$="value"][attr*="value"][attr|="value"]:first-child, :last-child:nth-child(n), :nth-last-child(n):only-child:empty:not(selector)div p (p anywhere inside div)div > p (p direct child of div)div + p (p immediately after div)div ~ p (p after div, same parent) let doc = Html5rw.parse reader in
(* Find all paragraphs *)
let paragraphs = Html5rw.query doc "p" in
(* Find links with specific class *)
let links = Html5rw.query doc "a.external" in
(* Find table cells in rows *)
let cells = Html5rw.query doc "tr > td" in
(* Check if a node matches *)
let is_active = Html5rw.matches node ".active"Raised when a selector string is malformed.
The exception contains an error message describing the parse error.
module Ast : sig ... endAbstract syntax tree for parsed selectors.
module Token : sig ... endToken types for the selector lexer.
val parse : string -> Ast.selectorParse a CSS selector string.
Query the DOM tree with a CSS selector.
Returns all nodes matching the selector in document order.
let divs = query root_node "div.content > p"val matches : Dom.node -> string -> boolCheck if a node matches a CSS selector.
if matches node ".active" then
(* node has class "active" *)