xpath icon indicating copy to clipboard operation
xpath copied to clipboard

last() not working on subgroups

Open ale-rinaldi opened this issue 2 years ago • 5 comments

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!

ale-rinaldi avatar Jun 03 '22 00:06 ale-rinaldi

@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

ale-rinaldi avatar Jul 07 '22 13:07 ale-rinaldi

Okay, I'm trying to fix this, it take a while.

zhengchun avatar Jul 07 '22 15:07 zhengchun

@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

zhengchun avatar Jul 08 '22 15:07 zhengchun

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!

ale-rinaldi avatar Jul 08 '22 16:07 ale-rinaldi

I'll try to fix another , It is more complex than subgroup query :smile:

zhengchun avatar Jul 08 '22 16:07 zhengchun

@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.

zhengchun avatar Jan 26 '23 11:01 zhengchun

Sorry @zhengchun for answering that late, and thank you very much for the fix, I can confirm it works like a charm!

ale-rinaldi avatar Feb 21 '23 00:02 ale-rinaldi