diff --git a/query.go b/query.go index fd05108..01c49f6 100644 --- a/query.go +++ b/query.go @@ -207,8 +207,10 @@ func (x *NodeNavigator) Value() string { return x.curr.Attr[x.attr].Value } return x.curr.InnerText() - case TextNode: + case TextNode, CharDataNode: return x.curr.Data + case DocumentNode: + return x.curr.InnerText() } return "" } diff --git a/xpath_string_values_test.go b/xpath_string_values_test.go new file mode 100644 index 0000000..b8c3b15 --- /dev/null +++ b/xpath_string_values_test.go @@ -0,0 +1,27 @@ +package xmlquery + +import ( + "github.com/antchfx/xpath" + "strings" + "testing" +) + +func TestXPathDocumentAndCDATAStringValues(t *testing.T) { + doc, err := Parse(strings.NewReader(`beforeafter`)) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + expr string + want interface{} + }{ + {`string(/)`, "before42after"}, {`string(//item/text())`, "42"}, {`number(//item/text())`, float64(42)}, {`boolean(//item/text()[.='42'])`, true}, {`string(//tail/text())`, "after"}, {`string(//comment())`, "excluded"}, + } { + t.Run(tc.expr, func(t *testing.T) { + got := xpath.MustCompile(tc.expr).Evaluate(CreateXPathNavigator(doc)) + if got != tc.want { + t.Errorf("got %#v, want %#v", got, tc.want) + } + }) + } +}