Skip to content

Commit

Permalink
fix XPath name(),local-name function query.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Jan 8, 2020
1 parent 054c26a commit 168353c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
23 changes: 5 additions & 18 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (b *builder) processFunctionNode(root *functionNode) (query, error) {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: notFunc}
case "namespace-uri":
case "name", "local-name", "namespace-uri":
if len(root.Args) > 1 {
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName)
}
Expand All @@ -304,27 +304,14 @@ func (b *builder) processFunctionNode(root *functionNode) (query, error) {
return nil, err
}
}
qyOutput = &functionQuery{Input: b.firstInput, Func: namespaceFunc(arg)}
case "name", "local-name":
inp := b.firstInput
if len(root.Args) > 1 {
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName)
}
if len(root.Args) == 1 {
argQuery, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
inp = argQuery
}
f := &functionQuery{Input: inp}
switch root.FuncName {
case "name":
f.Func = nameFunc
qyOutput = &functionQuery{Input: b.firstInput, Func: nameFunc(arg)}
case "local-name":
f.Func = localNameFunc
qyOutput = &functionQuery{Input: b.firstInput, Func: localNameFunc(arg)}
case "namespace-uri":
qyOutput = &functionQuery{Input: b.firstInput, Func: namespaceFunc(arg)}
}
qyOutput = f
case "true", "false":
val := root.FuncName == "true"
qyOutput = &functionQuery{
Expand Down
42 changes: 28 additions & 14 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,39 @@ func roundFunc(q query, t iterator) interface{} {
}

// nameFunc is a XPath functions name([node-set]).
func nameFunc(q query, t iterator) interface{} {
v := q.Select(t)
if v == nil {
return ""
}
ns := v.Prefix()
if ns == "" {
return v.LocalName()
func nameFunc(arg query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var v NodeNavigator
if arg == nil {
v = t.Current()
} else {
v = arg.Select(t)
if v == nil {
return ""
}
}
ns := v.Prefix()
if ns == "" {
return v.LocalName()
}
return ns + ":" + v.LocalName()
}
return ns + ":" + v.LocalName()
}

// localNameFunc is a XPath functions local-name([node-set]).
func localNameFunc(q query, t iterator) interface{} {
v := q.Select(t)
if v == nil {
return ""
func localNameFunc(arg query) func(query, iterator) interface{} {
return func(q query, t iterator) interface{} {
var v NodeNavigator
if arg == nil {
v = t.Current()
} else {
v = arg.Select(t)
if v == nil {
return ""
}
}
return v.LocalName()
}
return v.LocalName()
}

// namespaceFunc is a XPath functions namespace-uri([node-set]).
Expand Down

0 comments on commit 168353c

Please sign in to comment.