Hello,
I might be misusing xpath, but I'm not able to use the name() function on the ancestor-or-self::* axis, is this expected?
I'm trying to use the following query: string-join(ancestor-or-self::*[position()<last()]/name(), '.')
The returned error is string-join(ancestor-or-self::*[position()<last()]/name(), '.') has an invalid token.
I would expect my.sub.key and my2.sub2.key2 to be returned.
Repro:
package main
import (
"fmt"
"strings"
"github.com/antchfx/jsonquery"
"github.com/antchfx/xpath"
)
func main() {
s := `{
"my": { "sub": { "key": "value" } },
"my2": { "sub2": { "key2": "value" } }
}
`
doc, err := jsonquery.Parse(strings.NewReader(s))
if err != nil {
panic(err)
}
leafs, err := jsonquery.QueryAll(doc, "//*[not(*)]")
if err != nil {
panic(err)
}
for _, selectedfield := range leafs {
expr, err := xpath.Compile("string-join(ancestor-or-self::*[position()<last()]/name(), '.')")
if err != nil {
panic(fmt.Errorf("failed to compile: %w", err))
}
elt := jsonquery.CreateXPathNavigator(selectedfield)
fmt.Printf("elt = %v\n", selectedfield.Data)
n := expr.Evaluate(elt)
iter, ok := n.(*xpath.NodeIterator)
if !ok {
fmt.Printf("res = %v\n", n)
continue
}
if iter.MoveNext() {
current := iter.Current()
fmt.Printf("res[0] = %v\n", current)
// debugf("res[0]-getvalue %v", current.(*jsonquery.NodeNavigator).GetValue())
// debugf("res[0]-value %v", iter.Current().Value())
continue
}
fmt.Println("no")
}
}
Hello,
I might be misusing xpath, but I'm not able to use the
name()function on theancestor-or-self::*axis, is this expected?I'm trying to use the following query:
string-join(ancestor-or-self::*[position()<last()]/name(), '.')The returned error is
string-join(ancestor-or-self::*[position()<last()]/name(), '.') has an invalid token.I would expect
my.sub.keyandmy2.sub2.key2to be returned.Repro: