Repository navigation
Preserve document and CDATA XPath string values - #143
Conversation
by-ae
left a comment
There was a problem hiding this comment.
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.
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 andnumber(//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(base9429919ed0a2c62f385c010f24095a149a2d672a):go test ./... -count=1passes.go test -race ./... -count=1passes.git diff --checkpasses.Checks above ran locally on macOS; hosted CI and maintainer review remain separate. Prepared and independently reviewed with Codex assistance.