Mental Model & Execution
The xsel2 DSL (.dsl files) is the declarative pipeline language for xsel2.
xsel2 DSL (.dsl) Lexical & Grammar Specification
Comments
Single-line (# or //) and multi-line (/* ... */) comments are stripped by the lexer (src/scraper/dsl/lexer.cpp):
# Hash single-line comment// C++ style single-line comment/* Multi-line block comment */Caveat: Block comments (
/* ... */) do not nest. An unterminated/*runs to EOF without error.
Variables (let) & String Interpolation
Top-level let statements bind any DSL value (string, int, float, bool, null, list, object, or enum identifier) to a variable name:
let title_sel = "h1.product-title";let base_url = "https://example.com"let keep_keys = ["title", "price"]let schema_patch = {source: "web", active: true}- Trailing
;onletstatements is optional. - Variables are evaluated in top-to-bottom declaration order. Re-declaring
let x = ...overwritesx. - String interpolation: Inside any
"..."or'...'string literal (including object keys and nested values),${var}substitutesvar.to_string().- Escape a literal
${...}with\${var}(which renders as${var}). - Caveats:
- Referencing an undefined variable in
${missing}throwsstd::runtime_errorat parse time. - Unterminated
${throwsstd::runtime_error. - Interpolating a list or object produces
<list>or<object>(becauseDslValue::to_string()is used). Pass list/object variables directly by identifier (e.g.,pick_keys(keep_keys)), not inside"${keep_keys}". - Passing an unquoted unknown identifier as an argument does not throw at parse time—it becomes a bare
Identifiervalue (used for enum names or string coercion viaas_str()).
- Referencing an undefined variable in
- Escape a literal
Scraper Declarations
A top-level scraper definition binds an identifier to a chain of pipeline steps:
# All four declaration styles are equivalent:product = Scraper().select("h1").text().trim().set("title");product: ScraperPy().select("h1").text().trim().set("title")product := Scraper().select("h1").text().trim().set("title")product = .select("h1").text().trim().set("title")- The assignment operator may be
=,:,:=, or even omitted beforeScraper()/ScraperPy(). Scraper(...)/ScraperPy(...)constructor arguments, if any, are parsed and ignored.- Trailing
;is optional. - Redefining the same scraper name overwrites the previous definition (
insert_or_assign). - Implicit root extractor: If a chain does not start with
select(...)or$, the first non-selectstep implicitly creates anExtractortargeting the document root (select("")).
Literals, Containers & Enums
| Type | Syntax Examples | Notes & Caveats |
|---|---|---|
| String | "hello", 'hello' |
Supports \n, \t, \r, \$, \\, \", \' (any \x other than n/t/r/$ yields x). Unterminated string throws. |
| Integer | 0, 42, -1 |
Parsed as 32-bit signed int (std::from_chars). |
| Float | 1.5, -0.25, 1e5, 2.5e-3 |
Parsed as IEEE-754 double. Non-finite values (inf, nan) throw. |
| Boolean | true, false |
Case-sensitive lowercase keywords. |
| Null | null |
Mapped to std::monostate (or JSON null). |
| List | ["a", "b", 1], ["a",] |
Trailing comma before ] is allowed. |
| Object | {"old": "new", count: 1,} |
Keys can be quoted strings (with ${var} interpolation) or bare identifiers. Trailing comma before } is allowed. |
| Value enum | Value.Text, Text, text |
See mapping below. |
Value enum accepted spellings (case-sensitive):
Value.Text|Text|text→ extracts full subtree text (node.Text())Value.OwnText|OwnText|own_text→ extracts direct text children only (node.OwnText())Value.Attribute|Attribute|attr→ extracts attributeattr(node.Attribute(attr))Value.ElementExists|ElementExists|exists→"true"if matched,"false"if emptyValue.AttributeExists|AttributeExists|attr_exists→"true"if matched and hasattr,"false"otherwiseValue.Adjacent|Adjacent|adjacent→ searchesnode.NextSiblings()and extracts text
Nesting limit:
MAX_DSL_DEPTH = 256applies to nested lists/objects, grouped.( ... )chains, and{ ... }blocks. Exceeding 256 throwsstd::runtime_error.
Arguments: Positional vs. Keyword
Every step accepts positional arguments, keyword arguments (name=value), or a mix where positional arguments come first:
json_parse("cfg", allow_comments=true, allow_trailing_comma=true)Strict validation rules (src/scraper/dsl/value.cpp):
- No positional after keyword:
select("h1", key="a", "b")throws. - No duplicate keyword:
select("h1", key="a", key="b")throws. - No dual positional + keyword: passing the same parameter both positionally and by name throws.
- No extra positional or unknown keyword: passing too many positional args or an unrecognized
kwarg=...throws. - Scalar list coercion: steps expecting a string list (e.g.,
filter,pick_keys,omit_keys,js_blocks,extract_many,merge_keys,strip_query) accept either["a", "b"]or a single scalar"a"(automatically wrapped into["a"]). - JSON document arguments: steps expecting a JSON document (
json_schema,cast_keys,replace({...})) accept either a DSL object/list literal ({...}/[...]) or a JSON string literal ('{"a": 1}').
Execution, State & Merge Model
Pipeline Architecture
flowchart LR Doc["HTML / XML Document"] --> Ext["Extractor Group: select(selector, key)"] Ext -->|"0 matches"| Skip["Skip Extractor"] Ext -->|"N >= 1 matches"| Loop["For each matched Node"] Loop --> State["Init values=[], item=null, ctx"] State --> Steps["Run steps in order (including block-local dict/JSON transforms)"] Steps -->|"Step throws"| Err["ErrorPolicy: log/collect/throw + values.clear()"] Steps -->|"skip_item"| NextNode["Drop item, continue next Node"] Steps -->|"abort_scraper"| Fin["Run top-level / finalize dict transforms (always runs once)"] Steps --> Merge["merge_item_at_path(dest, key_segments, item)"] Merge --> KV["preprocess_key_value(dest) (@key/@value)"] KV --> Fin Fin --> Out["Final JSON Output"]
Per-Node State: values vs. dict
Inside each Extractor match, two mutable state objects exist (alongside a per-node ExecutionContext holding save/restore slots and skip_item/abort_scraper guard flags):
values(std::vector<std::string>): The working list of extracted/transformed strings.- Extraction steps (
text,attr,html,outer_html,tag_name,classes,find,find_all,xpath,xpath3) append tovalues. - Transform steps (
trim,squish,lower,upper,replace,regex_replace,prepend,append,substring,unescape,unescape_html,strip_comments,strip_tags,base64_*,hash,slugify,resolve_url,url_part,query_param,strip_query,unicode_normalize,strip_accents,map_values) mutatevaluesin place. - Filtering/slicing steps (
drop_empty,regex_filter,filter,filter_urls,fuzzy_filter,first,last,nth,slice,unique,sort,reverse,default) prune, reorder, or fallbackvalues. - Expansion/reduction steps (
split,join,regex_extract,regex_find_all,extract_many,js_block,parse_srcset,css_url) replacevalueswith a new vector. - Strict Sink Rule (writing to
dictclearsvalues): Every step that readsvaluesand writes todictclearsvalues—usetee { ... }if you need to keepvaluesfor another step:- Always write to
dictand clearvalues:set,append_to,count,js_blocks,parse_js,json_parse,csv_parse,regex_groups,query_params. - Dual-mode steps (
parse_number,parse_duration,to_int,to_float,to_bool,similarity,table,parse_date): whenkey == "", transformvaluesin place (or push serialized JSON fortable); whenkey != "", write native JSON values todict[key]and clearvalues.
- Always write to
- Direct DOM-to-
dictsteps (write todictwithout reading or modifyingvalues):attrs,extract_meta,extract_fields(fields),each/ nestedselect(...) { ... }.
- Extraction steps (
dict(jsoncons::json, initiallynull): The local JSON object for the current matched node.
Important: If a top-level extractor’s
select(selector)matches 0 nodes, its block-local steps do not run. However, top-level dict/JSON transforms (placed outside{ ... }blocks) andfinalize { ... }blocks always run once after all extractors finish.
Block-Local vs. Document-Level Execution
All dictionary and JSON transformation steps (flatten, pick_keys, omit_keys, rename_keys, merge_keys, sort_array, deduplicate, jmespath, jsonpath, json_schema, cast_keys, zip_keys, coalesce_keys) execute immediately without hidden deferral flags:
- Inside
{ ... }blocks or.( ... )grouped chains: Each dict/JSON transform runs in-order on the current node’s localdict(dict = step.transform(dict)). - At the top level of a scraper chain (outside
{ ... }and.( ... )) or insidefinalize { ... }: Dict/JSON transforms are appended tofinalize_steps_, so they execute once on the final accumulateddestdocument after all extractors finish—even if a precedingselect(...)matched 0 nodes.
Dot-Paths, Collision Modes & Merging
- Dot-path keys: Both
select(selector, key)andset(key)/insert_into_jsonsplitkeyon.into nested objects (e.g.,"meta.author"→{"meta": {"author": ...}}). Escape a literal dot with\.(e.g.,"a\\.b"in DSL string →a.bas a single key).NOTE
Dot-path splitting applies to extractor target keys and
coalesce_keyssource candidates. Dict transformation steps likepick_keys,omit_keys,rename_keys,cast_keys, andmerge_keysdo not parse dot-paths ("a.b"). Instead,pick_keys,omit_keys, andrename_keysoperate recursively matching bare key names across all nesting levels. - Empty value skipping: By default (
allow_empty=false),insert_into_jsonandmerge_intoignorenull,"",[], and{}(is_empty_json). - Single vs. Multiple values in
set:- 0 non-whitespace values → nothing is written (unless
allow_empty=trueonInsertOptionsin C++/Python). - 1 value → stored as a scalar.
- 2+ values → stored as a JSON array.
- 0 non-whitespace values → nothing is written (unless
- Single vs. Multiple node matches in
select:- When
selectmatches 1 node, itsitemobject is merged intodest(ordest[key]). - When
selectmatches multiple nodes with overlapping keys (e.g. two<div class="card">elements both setting"title"),merge_intodetects the common key and automatically combines the node objects into a JSON array ([{title: ...}, {title: ...}]). - If two extractors set disjoint keys on the root object,
merge_intomerges them into the same root object.
- When
- Key collision inside a single
dict(set/count): DefaultInsertMode::DefaultusesUniqueSuffix(key,key_0,key_1, …).
@key / @value Post-Processing
preprocess_key_value runs during merge_into and once at the end of Scraper::perform_extraction:
- If any JSON object contains both
"@key"and"@value", those two fields are removed and replaced with[json_to_string(@key)]: @value(skipped if@keyis empty or@valueis empty). - If an object contains only
"@key"or only"@value", the dangling@key/@valuefield is erased. - This allows scraping dynamic key-value pairs (e.g.
dt/ddorth/td) by setting"@key"and"@value"on each item!
Error Policies & Recovery (find(...) + ?? vs. required=true)
Extractor::extract supports three ErrorPolicy modes (configured in CLI/Python/C++):
LogAndContinue(default): Logs[prefix] Extractor error (name): ...tostderr, clearsvalues, and continues to the next step.CollectErrors: Appends the error string toerrors(), clearsvalues, and continues.ThrowOnError: Rethrows immediately, aborting the extraction.
Idiom & Caveat:
findandfind_allare optional by default (required=false), sofind(".x") ?? "N/A"works cleanly for normal fallbacks without throwing or logging tostderr. Only passrequired=truewhen you genuinely want missing fields flagged—for example, to abort underThrowOnError, record an error underCollectErrors, or log[prefix] Extractor error (name): ...tostderrunderLogAndContinue(which also clearsvaluesso a subsequent?? "N/A"can still recover).
Engines, Limits & Caveats
| Engine / Library | Used By | Key Caveats & Limits |
|---|---|---|
lxml2-query + libxml2 |
select, find, find_all, extract_fields, xpath, xpath3, resolve_url, url_part, query_param, query_params, strip_query, filter_urls, table, extract_meta, each, when_exists, unless_exists, skip_if_exists, abort_if_exists |
libxml2 HTML parser is lenient; CSS selectors support standard CSS3 (>, +, ~, [attr=val], :nth-child, etc.). xpath evaluates XPath 1.0 relative to the current node; xpath3 evaluates XPath 3.1 via lxml2-query when XSEL_USE_XPATH3=ON (default). query_params and strip_query also tolerate unencoded [ / ] in query strings. |
| RE2 | regex_replace (sub), regex_filter (grep), regex_extract (extract), regex_find_all, regex_groups, when_match, css_url |
Linear-time, guaranteed no catastrophic backtracking (no lookahead/lookbehind/backreferences). Default is case-sensitive (flags=null/""); pass "icase" or "i" for case-insensitive matching, "dotall" or "s" for . matching \n, and "multiline" or "m" for line-anchored ^/$. |
| Compressed-DFA Aho-Corasick | filter, replace({...}), extract_many |
Single-pass $O(n + m)$ literal matching. Mapping-mode replace({old: new}) resolves overlapping matches by leftmost-longest. |
| Heuristic JS/JSON Digger | js_block, js_blocks, parse_js, json_parse(..., lenient=true) |
Does not execute JavaScript; heuristically extracts object/array literals, strips // and /* */ comments, folds +-joined strings, expands JSON.parse("..."), and maps undefined → null. |
jsoncons |
jmespath, jsonpath, json_schema, json_parse, json_dump, csv_* |
MAX_JSON_DEPTH = 512 in xsel::common; json_parse defaults max_nesting_depth = 1024. |
absl::Time |
parse_date |
Auto-detects 15 common ISO/RFC/web date formats or uses explicit strftime fmt. Timezone names use IANA tzdb (e.g. "America/Sao_Paulo", "UTC"). |
utf8proc |
unicode_normalize, strip_accents |
Full Unicode NFC/NFD/NFKC/NFKD and combining mark (Mn) stripping. |
Fuzzy (xsel::common) |
fuzzy_filter, similarity |
UTF-8 codepoint-aware jaro_winkler (jw) and levenshtein (lev, edit). Caveat: case_sensitive=false folds ASCII A-Z to a-z only (not full Unicode case folding). |
Running & Embedding DSL
- CLI (
xsel2):See CLI Reference.Terminal window ./build/xsel2 -d examples/product.dsl -n product --html page.htmlcat page.html | ./build/xsel2 -d examples/product.dsl -n product -c./build/xsel2 -d examples/product.dsl -n product --batch pages/*.html -j 0 - Python (
scraperpy):Every DSL method maps 1:1 to aimport scraperpyscrapers = scraperpy.parse_dsl_string(dsl_text) # or load_scrapers("examples/product.dsl")result = scrapers["product"].from_string(html)ScraperPymethod. See Python Bindings. - C++20 (
scraper::dsl::DslParser):In C++scraper::dsl::DslParser parser;auto scrapers = parser.parse_file("examples/product.dsl");jsoncons::json out = scrapers.at("product").scrape_from_string(html);ScraperBuilder, method names usePascalCase(e.g.,select→Select,regex_extract→RegexExtract,json_parse→JsonParse). See C++20 API Guide.