Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

last() not working on subgroups #76

Closed
ale-rinaldi opened this issue Jun 3, 2022 · 7 comments
Closed

last() not working on subgroups #76

ale-rinaldi opened this issue Jun 3, 2022 · 7 comments
Labels

Comments

@ale-rinaldi
Copy link

ale-rinaldi commented Jun 3, 2022

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 zhengchun added the bug label Jun 3, 2022
@ale-rinaldi
Copy link
Author

@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

@zhengchun
Copy link
Contributor

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

@zhengchun
Copy link
Contributor

@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 dfece7e 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 #78

@ale-rinaldi
Copy link
Author

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!

@zhengchun
Copy link
Contributor

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

zhengchun added a commit that referenced this issue Jan 26, 2023
…k to `Clone()` method supports concurrent query. #76,#78
zhengchun added a commit that referenced this issue Jan 26, 2023
@zhengchun
Copy link
Contributor

@ale-rinaldi , Hello, The commit dfece7e 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.

@ale-rinaldi
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants