Giter Club home page Giter Club logo

Comments (8)

zhengchun avatar zhengchun commented on September 27, 2024

@jcamiel , I updated xpath package to fix this issue. Trying get the latest version of xpath package to solve.

from htmlquery.

jcamiel avatar jcamiel commented on September 27, 2024

Hi,

Thanks you for this really quick fix !
I've tested it and it's ok. Unfortunately, while I tried to debug it, I've found another possible issue (maybe related to the previous?):

<html>
	<body>
			<div class="fruit">Apple</div>
			<div class="color">Red</div>
	</body>
</html>

In this case, the following path string((//div[@class="color"])[1]) should return 'Red' (if I'm not mistaken). I've tested this one with your recent fix but it doesn't seem to work.

	html := `<html>
	<body>
			<div class="fruit">Apple</div>
			<div class="color">Red</div>
	</body>
	</html>`
	doc, _ := htmlquery.Parse(strings.NewReader(html))

	test1 := `string((//div[@class="color"])[1])`
	expr1, _ := xpath.Compile(test1)
	v1 := expr1.Evaluate(htmlquery.CreateXPathNavigator(doc))
	// v1 = "", should be Red ?

Regards,

jc

from htmlquery.

zhengchun avatar zhengchun commented on September 27, 2024

yes, should be Red. This bug still exist :(

from htmlquery.

zhengchun avatar zhengchun commented on September 27, 2024

@jcamiel , try again.

html := `<html><body>
	<div class="fruit">Apple</div>
	<div class="color">Red</div>
	<div>
	<div class="fruit">Pear</div>
	<div class="fruit">Orange</div>
	</div>
</body></html>`

doc, _ := htmlquery.Parse(strings.NewReader(html))
testPath := `//div[@class="color"][1]`
node := htmlquery.FindOne(doc, testPath)
fmt.Println(htmlquery.InnerText(node))
// Output: Red
testPath = `//div[@class="fruit"][1]`
node = htmlquery.FindOne(doc, testPath)
fmt.Println(htmlquery.InnerText(node))
// Output: Apple
testPath = `//div[1]`
for _, node := range htmlquery.Find(doc, testPath) {
	fmt.Println(htmlquery.InnerText(node))
	// Apple
	// Orange
}
testPath = `//div[2]`
for _, node := range htmlquery.Find(doc, testPath) {
	fmt.Println(htmlquery.InnerText(node))
	// Red
	// Pear
}

from htmlquery.

jcamiel avatar jcamiel commented on September 27, 2024

Hi @zhengchun
I've tested your last commit and it's ok but now I've still have another problem :( ! Maybe related still related to the [%n] operator I don't know :

func TestEvalXPathHTML(t *testing.T) {
	html := `<html><body>
				<div class="fruit">
					<div class="color">Red</div>
				</div>
				<div class="fruit">
					<div class="color">Yellow</div>
				</div>
		</body></html>`

	doc, _ := htmlquery.Parse(strings.NewReader(html))
	test := `count((//div[@class="fruit"])[1]//div[@class="color"])`
	expr, _ := xpath.Compile(test)
	v := expr.Evaluate(htmlquery.CreateXPathNavigator(doc))
	// v = 2, expected v = 1
}

I really appreciate your fixing and hope you don't mind me sending your this stuff. I've
a whole html page that I'm trying to analyse for my work and I try to isolate to a minimal sample the
xpath queries I think are not ok,

Regards,

Jc

from htmlquery.

zhengchun avatar zhengchun commented on September 27, 2024

I have trying [n] but found it is difficult. I can solved your //div[@class="fruit"])[1]//div[@class="color"]) but can't solve below example.

	html := `<html><body>
	<div class="fruit">Apple</div>
	<div class="color">Red</div>
	<div>
	<div class="fruit">Pear</div>
	<div class="fruit">Orange</div>
	</div>
	<div class="fruit">Peach</div>
</body></html>`
testPath := `//div[@class="fruit"][2]`

I have marked this issue.

from htmlquery.

jcamiel avatar jcamiel commented on September 27, 2024

Ok thank you very much, let me know if you have some code to test, I'll test it with my page,
Best regards,
Jc

from htmlquery.

zhengchun avatar zhengchun commented on September 27, 2024

fixed on [email protected]

from htmlquery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.