last() not working on subgroups
Hello,
first of all, thanks for your amazing work, it's really useful to me.
I'm having an issue when using the last() function on a subgroup, for example on a result of another filter.
For example, this piece of code:
package main
import (
"fmt"
"github.com/antchfx/xmlquery"
"strings"
)
func main() {
xml := `
<root>
<test foo="bar">First</test>
<test foo="bar">Second</test>
<test foo="xyz">Third</test>
</root>
`
doc, err := xmlquery.Parse(strings.NewReader(xml))
if err != nil {
panic(err)
}
tests, err := xmlquery.QueryAll(doc, `(//test[@foo="bar"])[last()]`)
if err != nil {
panic(err)
}
for _, test := range tests {
fmt.Println(test.InnerText())
}
}
I would expect this to print "Second", but it prints nothing, because the output from the query has no elements.
Am I doing something wrong, or is this an issue on the last() function?
Thank you!
@zhengchun sorry to bother you, do you think you'll have the occasion to solve this in the near future? I tried to have a look at it by myself but I really couldn't figure it out. Thank you
Okay, I'm trying to fix this, it take a while.
@ale-rinaldi , last() is a special function, it needs to knows how many matching nodes , The value is dynamic unlike the [1],[2] is pre-defined position on query.
I try add a new class cacheQuery that is to pre-caching all matched nodes and then expose a count method used by last().
This commit https://github.com/antchfx/xpath/commit/dfece7e444f00ae3aea4086a84ba9afdc0b93783 just put groupquery into cachequery to fix your (//test[@foo="bar"])[last()] , but not fix the another bug like //test[@foo="bar"][last()]. This bug I create a new issue on https://github.com/antchfx/xpath/issues/78
Thank you very much, having a way to do that, without other code than the xpath itself, is enough for my use case. Subgrouping is not a problem 👍
Thanks again!
I'll try to fix another , It is more complex than subgroup query :smile:
@ale-rinaldi , Hello, The commit https://github.com/antchfx/xpath/commit/dfece7e444f00ae3aea4086a84ba9afdc0b93783 will causing the deadlock in the multiple goroutines. Please update your package to the the latest version https://github.com/antchfx/xpath/releases/tag/v1.2.3, it fixed the deadlook bug and last() query.
Sorry @zhengchun for answering that late, and thank you very much for the fix, I can confirm it works like a charm!