Skip to content

xpath: leave DOCTYPE nodes out of node-sets - #86

Open
dkam wants to merge 1 commit into
jonwiggins:mainfrom
dkam:xpath-doctype-nodes
Open

dkam wants to merge 1 commit into
jonwiggins:mainfrom
dkam:xpath-doctype-nodes

Conversation

@dkam

@dkam dkam commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

node() matches DOCTYPE nodes, so they turn up in XPath results. XPath's data model has no DOCTYPE node, and libxml2 never returns one:

let doc = Document::parse_str("<!DOCTYPE r [<!ELEMENT r ANY>]><?p?><r/>").unwrap();
Expression main libxml2 2.14 and 2.15, and this PR
/node() DOCTYPE, <?p?>, <r/> <?p?>, <r/>
/node()[1] DOCTYPE <?p?>
count(/node()) 3 2

The same goes for //node(), both sibling axes, preceding:: and following::. Every HTML document is affected, because parse_html adds a DOCTYPE when the input has none.

The change

node() no longer matches a DOCTYPE, except on the self axis. That axis only reaches a DOCTYPE that is itself the context node, and there libxml2's . returns it. xmloxide parses . as self::node(), so filtering the self axis as well would break .. One small difference remains: with a DOCTYPE as the context node, libxml2 returns nothing for an explicit self::node(), and this returns the DOCTYPE.

Tests

Three new tests in xpath/eval.rs. Two failed on main and pass now. They cover /node(), /node()[2], count(/node()), both sibling axes, preceding:: (from an element and from an attribute), following::, /descendant-or-self::node(), //node(), and parse_html with and without a DOCTYPE. The third, which passed before as well, checks that . and .. still work with a DOCTYPE as the context node. Every expectation was checked against libxml2 2.14.6 and 2.15.4.

All 1,670 tests pass, as do W3C 1727/1727 and libxml2 119/119. Clippy and docs are clean.

Cost

Instructions per query, counted with callgrind on parser_bench's XPath fixture:

  • parser_bench's six XPath queries: unchanged, within ±0.01%.
  • Queries that test node() on every node pay about 5 instructions per node for the extra check, as libxml2 does: //node() +6.4%, count(//node()) +6.0%, //book[40]/following::node() +1.2%, //text()/.. +0.3%.
  • Timed as the median of 21 alternating runs: //node() +4.8%, count(//node()) +3.7%, //book/title −0.2%.

Related, not in this PR

When entities aren't substituted, libxml2's XPath also skips entity-reference nodes and doesn't descend into them. xmloxide selects both: with <!DOCTYPE r [<!ENTITY e "x<i/>">]><r>a&e;b</r>, count(//i) is 1 where libxml2 gives 0. I can open an issue if you'd like that to match too.

This came up while comparing against libxml2 for the Rust Nokogiri experiment mentioned in #84.

🤖 Generated with Claude Code

XPath's data model has no DOCTYPE node, and libxml2's XPath never
selects one, but node() matched it. So /node(), //node(),
count(/node()), positions such as /node()[2], the sibling axes and
preceding:: and following:: all included the DOCTYPE. Every HTML
document has one, because parse_html adds one when the input
doesn't.

node() no longer matches a DOCTYPE, except on the self axis. That
axis only reaches a DOCTYPE that is itself the context node, and
libxml2's `.` keeps it there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant