Code Monkey home page Code Monkey logo

Comments (6)

zhengchun avatar zhengchun commented on June 12, 2024

@mildred , Sorry for the late reply .

I don't think this is a problem. /foo/bar@attr expression means searchs bar element that have a attr name.

The package Expr have two methods: Seelct and Evaluate. if you want matches attr="b", you can consider Evaluate method. #4 (comment)

expr, err := xpath.Compile("//img/@src")
val := expr.Evaluate(nav)
switch typ := val.(type) {
case *xpath.NodeIterator:
    for typ.MoveNext() {
	fmt.Println(typ.Current().Value()) // output @src value
    }
}

from xpath.

mildred avatar mildred commented on June 12, 2024

Thank you, but the test I made here uses Evaluate and not Select, but the result is not the attribute node but the element. Am I missing something?

from xpath.

zhengchun avatar zhengchun commented on June 12, 2024

The /foo/bar@attr change to /foo/bar/@attr, I think expression /foo/bar@attr and /foo/bar/@attr is different results.

expr, _ := xpath.Compile("//foo/bar/@attr")
iter := expr.Evaluate(xmlquery.CreateXPathNavigator(doc))
switch typ := iter.(type) {
case *xpath.NodeIterator:
	for typ.MoveNext() {
		fmt.Printf("%v:%s\n", typ.Current().NodeType(), typ.Current().LocalName())
	}
}

from xpath.

mildred avatar mildred commented on June 12, 2024

Good to know. Then the question is which behaviour confirms with the XPath W3C recommendation. Do you have a goal to follow the recommendation closely or not?

When looking through the recommendation, the only occurence of node/@attr is for ../@attr. It seems for non parent elements, they use elem@attr directly.

from xpath.

zhengchun avatar zhengchun commented on June 12, 2024

I'm test foo/bar@attr expression on xpath-tester, got parse error:
Unable to parse any XML input. Unexpected token "@" beyond end of expression

This error also occurs on .NET when using XML library.

So the elem@attr looks is not correct syntax. xpath package should be return error when parsing this expression, but it not.

from xpath.

mildred avatar mildred commented on June 12, 2024

You are completely right, and I apologize for the issue that is not an issue. I tested this expression somewhere and I assumed that because it worked there, it was valid XPath.

from xpath.

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.