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

replace() on a query doesn't seem to work #51

Closed
alecthomas opened this issue Sep 12, 2022 · 3 comments
Closed

replace() on a query doesn't seem to work #51

alecthomas opened this issue Sep 12, 2022 · 3 comments

Comments

@alecthomas
Copy link

package main

import (
	"fmt"
	"strings"

	"github.com/antchfx/htmlquery"
)

func main() {
	s := `<html><a href="https://github.com/cashapp/hermit-build/releases/download/go-tools/stringer-v0.1.12-darwin-amd64.bz2">foo</a></html>`
	doc, err := htmlquery.Parse(strings.NewReader(s))
	if err != nil {
		panic(err)
	}
	nodes, err := htmlquery.QueryAll(doc, `replace((//a[contains(@href, '/stringer-')])/@href, '^.*/stringer-v([^-]*)-.*$', '$1')`)
	if err != nil {
		panic(err)
	}
	for _, node := range nodes {
		fmt.Println(htmlquery.OutputHTML(node, false))
	}
}

On playground: https://go.dev/play/p/jxU6UgH0DnK
The same content+query works fine on https://www.freeformatter.com/xpath-tester.html

The above example without replace() works fine: https://go.dev/play/p/N22KULbkgRu

@alecthomas alecthomas changed the title contains() on a query doesn't seem to work replace() on a query doesn't seem to work Sep 12, 2022
@zhengchun
Copy link
Contributor

zhengchun commented Sep 12, 2022

There are two problem.

  1. replace() not supportd the regex syntax.
  2. replace() as a function and return a value with string type, you should call xpath.Evaluate(...), the htmlquery.QueryAll() always return a set of node.
expr, err := xpath.Compile(`replace((//a[contains(@href, '/stringer-')])/@href, '^.*/stringer-v([^-]*)-.*$', '$1')`)
if err != nil {
	panic(err)
}
v := expr.Evaluate(htmlquery.CreateXPathNavigator(doc))
fmt.Println(v.(string))

@alecthomas
Copy link
Author

Thank you for the pointers, I've switched to using substring-after and substring-before. BTW it looks like replace should support regex?

@alecthomas
Copy link
Author

Thanks for a great set of libraries BTW, really nicely done.

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

No branches or pull requests

2 participants