Skip to content

Commit 1eb2707

Browse files
author
Denys Smirnov
committed
fix name() function
Update name() function to accept a node set as an argument. Also, the name() function returns a namespace+local-name according to the spec. The change fixes function behavior as well.
1 parent f671257 commit 1eb2707

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

build.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,18 @@ func (b *builder) processFunctionNode(root *functionNode) (query, error) {
234234
}
235235
qyOutput = &functionQuery{Input: argQuery, Func: notFunc}
236236
case "name":
237-
qyOutput = &functionQuery{Input: b.firstInput, Func: nameFunc}
237+
inp := b.firstInput
238+
if len(root.Args) > 1 {
239+
return nil, errors.New("xpath: name function must have at most one parameter")
240+
}
241+
if len(root.Args) == 1 {
242+
argQuery, err := b.processNode(root.Args[0])
243+
if err != nil {
244+
return nil, err
245+
}
246+
inp = argQuery
247+
}
248+
qyOutput = &functionQuery{Input: inp, Func: nameFunc}
238249
case "last":
239250
qyOutput = &functionQuery{Input: b.firstInput, Func: lastFunc}
240251
case "position":

func.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ func sumFunc(q query, t iterator) interface{} {
9292

9393
// nameFunc is a XPath functions name([node-set]).
9494
func nameFunc(q query, t iterator) interface{} {
95-
return t.Current().LocalName()
95+
v := q.Select(t)
96+
if v == nil {
97+
return ""
98+
}
99+
ns := v.Prefix()
100+
if ns == "" {
101+
return v.LocalName()
102+
}
103+
return ns + ":" + v.LocalName()
96104
}
97105

98106
func asBool(t iterator, v interface{}) bool {

xpath_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func TestOr_And(t *testing.T) {
192192
func TestFunction(t *testing.T) {
193193
testEval(t, html, "boolean(//*[@id])", true)
194194
testEval(t, html, "boolean(//*[@x])", false)
195+
testEval(t, html, "name(//title)", "title")
195196
testXPath2(t, html, "//*[name()='a']", 3)
196197
testXPath(t, html, "//*[starts-with(name(),'h1')]", "h1")
197198
testXPath(t, html, "//*[ends-with(name(),'itle')]", "title") // Head title

0 commit comments

Comments
 (0)