Skip to content

Preserve document and CDATA XPath string values - #143

Merged
zhengchun merged 1 commit into
antchfx:masterfrom
rudycelekli:fix/xpath-string-values-20261101
Nov 7, 2026
Merged

zhengchun merged 1 commit into
antchfx:masterfrom
rudycelekli:fix/xpath-string-values-20261101

Conversation

@rudycelekli

Copy link
Copy Markdown
Contributor

XPath treats CDATA as text and gives a document node the concatenated descendant text value. The navigator currently returns an empty string for both, making string(/) empty and number(//item/text()) NaN for a CDATA value of42. Return the existing text data/InnerText for these node types. Tests retain ordinary text and comment controls. Oracle: https://www.w3.org/TR/1999/REC-xpath-19991116/#data-model .

Validation at 757a2950e1c47bd7a89659df3f5a90c53d233e0f (base 9429919ed0a2c62f385c010f24095a149a2d672a):

  • Added regression fails on the unchanged base.
  • go test ./... -count=1 passes.
  • go test -race ./... -count=1 passes.
  • git diff --check passes.

Checks above ran locally on macOS; hosted CI and maintainer review remain separate. Prepared and independently reviewed with Codex assistance.

@by-ae by-ae left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked at 757a295 against base 9429919. go test ./... passes, and TestXPathDocumentAndCDATAStringValues is the only thing that changes behaviour on the branch (it fails on base for string(/), string(//item/text()), number(...) and the .='42' predicate).

I also ran a document with everything at the top level, <?xml version="1.0"?><!DOCTYPE r><!--top--><?pi data?><r>a<![CDATA[<b>&]]>c<k>x</k><?pi2 d?></r><!--end-->. On the branch string(/) is a<b>&cx (base: empty), the same as string(/r), so the declaration, DOCTYPE, comments and PI data are correctly left out of the document string value, and contains(/, 'x') and normalize-space(/) work too. The DocumentNode case in query.go#L213-L214 relies on InnerText skipping comments and non-text nodes (node.go#L140-L157), which is what the XPath data model needs. The new test input has no declaration, DOCTYPE or PI, though, so adding those to the string(/) case (<?xml version="1.0"?><!--top--><?pi data?> in front of <root>) would lock in that they are excluded.

Two things I noticed that are not caused by this PR, so only as follow-up notes: CDATA is still a separate text() node. <k>a<![CDATA[b]]>c</k> gives count(//k/text()) = 3 and number(//k/text()[1]) = NaN, where the XPath data model has one merged text node, and <k><![CDATA[4]]><![CDATA[2]]></k> gives number(//k/text()) = 4, not 42. Also Value() returns "" for a processing instruction, whose string value is its data in XPath.

@zhengchun
zhengchun merged commit b98a791 into antchfx:master Nov 7, 2026
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.

3 participants