Skip to Content
PureHTMLdocs
Transformers

Transformers Overview

Transform, clean, parse, and re-format DOM data with composable transformers.

What are Transformers?

By default, CSS selectors extract the innerText of matching HTML elements as a string.

In real-world web data extraction, raw text often needs formatting: trimming whitespace, casting to numbers, parsing embedded JSON, extracting attribute values, or resolving relative URLs. Transformers provide a declarative, chained pipeline to perform these manipulations.

selector: .price
transform:
  - trim
  - 'replace("$", "")'
  - number

Argument Syntax & Quoting Rules

Transformers support parameter syntax with specific quoting rules:

  1. Bare (unquoted) arguments: Simple identifiers matching [\w-]+ (letters, digits, underscores, hyphens) can be written without quotes:

    • attr(href)
    • trim
    • split(-)
  2. Quoted arguments ("..." or '...'): Arguments containing spaces, commas, colons, or regex metacharacters must be enclosed in single or double quotes:

    • replace("\s+", " ")
    • split(", ")
    • join(" - ")
    • urlQueryParam("utm_source")
  3. Escaping inside quotes: A backslash escapes the enclosing quote character (\" inside "..." or \' inside '...') or a literal backslash (\\). Regex escape sequences like \s, \d, and \w are passed untouched to regex engines.

  4. YAML quoting tip: In YAML configurations, enclose the transform expression in quotes to avoid syntax collisions with YAML parsers:

    transform: 'replace("\s+", " ")'

Available Transformers

TransformerDescription
attrExtract element attributes
booleanCast values to boolean using truthy semantics
capitalizeUppercase the first character, lowercase the rest
existsCheck whether a selector matched any element
htmlExtract innerHTML of the element
joinJoin array elements into a string with a delimiter
jsonParse JSON string into an object or array
jsonpathQuery JSON structures using JSONPath syntax
lengthCount array items, object keys, or string length
lowerConvert string to lowercase
numberParse string into a numeric value
removeLastPathSectionRemove the trailing segment from a URL path
removeUrlQueryParamRemove query parameters from a URL
replaceReplace substrings or regex patterns
resolveResolve relative URLs against the document base URL
splitSplit string into an array by delimiter or whitespace
trimRemove leading and trailing whitespace
upperConvert string to uppercase
urlQueryParamExtract query parameters from a URL string

On this page