#1checkpoint_1
XPath Querying of XML from Stdin
CLI
Implement executable xjq.py:
uv run --project /app xjq.py [OPTIONS] QUERY [INFILE]
QUERY: XPath expression.INFILE: accepted positional argument but not used.- Input source: stdin.
Behavior
- Parse stdin as XML/HTML using case-sensitive element matching.
- Evaluate
QUERYwith XPath 1.0. - Exit code
0on successful execution, including zero matches.
Output
- Text/attribute results: strip each result, collapse internal whitespace runs to single spaces, then join results with each on a newline.
- XML node results: pretty-print serialized XML; when multiple XML nodes match, output only the first node.
- No-match output: write nothing to stdout/stderr.
Errors
- Invalid XPath: stderr message, exit code
1; message should referencexpath. - Malformed/empty XML input: stderr message, exit code
1; message should referencexml/parse.
#2checkpoint_2
CSS Selectors and Text Extraction
CLI Additions
--css: interpretQUERYas CSS selector.-t,--text: direct text extraction from matched elements.--text-all: descendant text extraction from matched elements.
CSS Query Mode
- Define custom
::textpseudo-element:selector::textreturns direct text;selector ::textreturns all descendant text nodes (one text node per line). - Comma-separated selectors are supported for
::textqueries when all selectors use the same::textmode. - Mixing direct and descendant
::textmodes in one comma-separated CSS query is invalid (stderr message, exit code1). - In CSS mode without text extraction, return XML node output with pretty-print serialization.
- Invalid CSS selector: stderr message, exit code
1.
Text Extraction
- Multi-match text output joins lines with
\n. - Strip each result and collapse internal whitespace runs to single spaces.
Flag Rules
- If both
--textand--text-allare present,--text-allwins. - If query already extracts text (
text()or::text),--textand--text-allare no-op modifiers.
#3checkpoint_3
JSON Input and Auto-Detection
Input Auto-Detection
- Auto-detect stdin as JSON object/array or XML/HTML.
- Top-level JSON primitives are not accepted as JSON input for this mode; they fall back to XML parsing.
- Input that fails JSON detection is parsed as XML/HTML.
JSON-to-XML Contract
- Wrap converted documents in
<root>. <root>must not includetype.- For top-level objects: keys become direct child elements.
- For top-level arrays: each entry becomes an
<item>child of<root>. - Object keys become element tag names (case preserved).
- Primitive values map to element text:
booleans as
true/false, numbers as minimal string form, null as empty. Examples:1 -> "1",1.0 -> "1",1.50 -> "1.5",0.00012 -> "0.00012". - Every converted element except
<root>carriestypein:str,int,float,bool,dict,list,null. - Preserve JSON key order in converted XML.
- JSON keys that are not valid XML element names are invalid input and must
produce stderr output with exit code
1(include terms likejson/key/invalid).
#4checkpoint_4
File Input, First-Result, and Compact Output
CLI Additions
-f,--first: return only first result.-c,--compact: compact output mode.
Input Source
INFILEtakes precedence over stdin when both are present.- Additional positional args after
QUERY [INFILE]are ignored. - Decode
INFILEas UTF-8 text; a UTF-8 BOM is allowed. - Apply JSON auto-detection to file input: treat as JSON object/array when detected, otherwise parse as XML/HTML.
- Missing/unreadable file: stderr message, exit code
1.
Output Rules
--firstapplies across text, attribute, and XML-node results.--firstwith no matches remains silent (no output).--compactaffects XML serialization only: compact XML output has no added pretty-print formatting.- For JSON-derived input,
--compactstill applies to resulting XML output.
#5checkpoint_5
Smart Output Formatting, JSON Export, and Multi-XPath
CLI Addition
-j,--json: JSON export mode for XML element results.
Output Precedence
When multiple output flags are present:
--text-all--text--json- default auto-format behavior
Notes:
--jsonhas no effect when results are already auto-detected as text (e.g., text()/attribute outputs).- In
--cssmode,--jsonhas no effect and output follows the normal CSS rules for the query.
JSON Export Contract
- Export shape: JSON array of objects
{tag_name: immediate_text_content}. - Use immediate text only (exclude descendant text from child elements).
- Default JSON formatting: 2-space indent.
- With
--compact, produce zero-indent JSON layout (no leading whitespace). - With
--first, still return a JSON array containing one exported element.
XPath Union
- Support XPath
|union in XPath mode. --textand--text-allapply across union queries.- For XPath union queries with active
--text-all, concatenate descendant text across all union matches into one normalized output string. - If
--firstis also active with union +--text-all, only the first matched union node contributes to the output. - If any union sub-path already performs text extraction, text flags are treated as no-op query modifiers.
- In CSS mode, pass
|through as CSS query text (no XPath union semantics). If the CSS engine rejects the selector, report a CSS selector error on stderr with exit code1.