added SelectNodesDirect function
This commit is contained in:
parent
89d27a9dee
commit
1145e04eb0
|
@ -86,6 +86,12 @@ func (this *Document) SelectNodes(namespace, name string) []*Node {
|
||||||
return this.Root.SelectNodes(namespace, name)
|
return this.Root.SelectNodes(namespace, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Select all nodes directly under this document, with the given namespace
|
||||||
|
// and name. Returns an empty slice if no matches were found.
|
||||||
|
func (this *Document) SelectNodesDirect(namespace, name string) []*Node {
|
||||||
|
return this.Root.SelectNodesDirect(namespace, name)
|
||||||
|
}
|
||||||
|
|
||||||
// Select all nodes with the given namespace and name, also recursing into the
|
// Select all nodes with the given namespace and name, also recursing into the
|
||||||
// children of those matches. Returns an empty slice if no matches were found.
|
// children of those matches. Returns an empty slice if no matches were found.
|
||||||
func (this *Document) SelectNodesRecursive(namespace, name string) []*Node {
|
func (this *Document) SelectNodesRecursive(namespace, name string) []*Node {
|
||||||
|
|
13
node.go
13
node.go
|
@ -397,6 +397,19 @@ func (this *Node) SelectNodes(namespace, name string) []*Node {
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Select multiple nodes directly under this node, by name.
|
||||||
|
func (this *Node) SelectNodesDirect(namespace, name string) []*Node {
|
||||||
|
list := make([]*Node, 0, 16)
|
||||||
|
|
||||||
|
for _, v := range this.Children {
|
||||||
|
if (namespace == "*" || v.Name.Space == namespace) && (name == "*" || v.Name.Local == name) {
|
||||||
|
list = append(list, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
// Select multiple nodes by name
|
// Select multiple nodes by name
|
||||||
func (this *Node) SelectNodesRecursive(namespace, name string) []*Node {
|
func (this *Node) SelectNodesRecursive(namespace, name string) []*Node {
|
||||||
list := make([]*Node, 0, 16)
|
list := make([]*Node, 0, 16)
|
||||||
|
|
38
xmlx_test.go
38
xmlx_test.go
|
@ -5,8 +5,8 @@
|
||||||
package xmlx
|
package xmlx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadLocal(t *testing.T) {
|
func TestLoadLocal(t *testing.T) {
|
||||||
|
@ -269,12 +269,12 @@ func TestElementNodeValueFetchAndSetIdentity (t *testing.T) {
|
||||||
textN.AddChild(namelessN)
|
textN.AddChild(namelessN)
|
||||||
|
|
||||||
targetN := rootN.SelectNode("", "text") // selects textN
|
targetN := rootN.SelectNode("", "text") // selects textN
|
||||||
if (targetN != textN) {
|
if targetN != textN {
|
||||||
t.Errorf("Failed to get the correct textN, got %#v", targetN)
|
t.Errorf("Failed to get the correct textN, got %#v", targetN)
|
||||||
}
|
}
|
||||||
|
|
||||||
// targetN.Value is empty (as the value lives in the childNode)
|
// targetN.Value is empty (as the value lives in the childNode)
|
||||||
if (targetN.Value != "") {
|
if targetN.Value != "" {
|
||||||
t.Errorf("Failed to prepare correctly, TargetN.Value is not empty, it contains %#v", targetN.Value)
|
t.Errorf("Failed to prepare correctly, TargetN.Value is not empty, it contains %#v", targetN.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -337,7 +337,6 @@ func TestElementNodeValueFetchAndSet(t *testing.T) {
|
||||||
<found />
|
<found />
|
||||||
ue</available></car>`
|
ue</available></car>`
|
||||||
|
|
||||||
|
|
||||||
if got := doc.Root.String(); got != expected {
|
if got := doc.Root.String(); got != expected {
|
||||||
t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
|
t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
|
||||||
}
|
}
|
||||||
|
@ -368,3 +367,34 @@ func TestElementNodeValueFetchAndSet(t *testing.T) {
|
||||||
t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
|
t.Fatalf("expected: \n%s\ngot: \n%s\n", expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSelectNodesDirect(t *testing.T) {
|
||||||
|
data := `<root><wrapper><hidden></hidden>
|
||||||
|
<hidden></hidden></wrapper></root>`
|
||||||
|
doc := New()
|
||||||
|
|
||||||
|
if err := doc.LoadString(data, nil); nil != err {
|
||||||
|
t.Fatalf("LoadString(): %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
root := doc.SelectNode("*", "root")
|
||||||
|
if root == nil {
|
||||||
|
t.Fatalf("Failed to get root, got nil, wanted Node{root}")
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes := root.SelectNodesDirect("*", "hidden")
|
||||||
|
|
||||||
|
if len(nodes) != 0 {
|
||||||
|
t.Errorf("SelectDirectNodes should not select children of children.")
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapper := root.SelectNode("*", "wrapper")
|
||||||
|
if wrapper == nil {
|
||||||
|
t.Fatalf("Failed to get wrapper, got nil, wanted Node{wrapper}")
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes = wrapper.SelectNodesDirect("*", "hidden")
|
||||||
|
if len(nodes) != 2 {
|
||||||
|
t.Errorf("Unexcepted hidden nodes found. Expected: 2, Got: %d", len(nodes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue