Skip to content

Xpath position function not working properly #40

@carlows

Description

@carlows

Hi there,

First of all, thank you for the packages, they're very useful 🚀

I've been having issues with the position function and I'm not sure if it's an issue with the htmlquery package or the xpath package, here's an example:

const htmlSample = `<!DOCTYPE html><html lang="en-US">
<head>
<title>Hello,World!</title>
</head>
<body>
<div class="test">
	<a href="/test1">Test 1</a>
</div>
<div class="test">
	<a href="/test2">Test 1</a>
</div>
<div class="test">
	<a href="/test3">Test 1</a>
</div>
</body>
</html>
`

func TestXPath(t *testing.T) {
	list := Find(testDoc, "//div[@class=\"test\" and position()=1]//a/@href")
	for _, n := range list {
		fmt.Println(InnerText(n))
	}
}

I would expect this to filter all the nodes that have the class test and have a position == 1, so only the first <a /> element. But instead, I get all the nodes. If I try position()=2 I get nothing back.

If I instead use this xpath, it gives me the correct element:

//div[@class=\"test\"][2]//a/@href

If I try this on the browser it works, so I'm not sure if it is expected that it works this way here 🤔.

What could be the problem?
Thank you again!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions