added SelectNodesDirect function

This commit is contained in:
Kevin Darlington 2015-02-03 13:30:24 -08:00
parent 89d27a9dee
commit 1145e04eb0
3 changed files with 57 additions and 8 deletions

View File

@ -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
View File

@ -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)

View File

@ -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) {
@ -256,9 +256,9 @@ func TestElementNodeValueFetch(t *testing.T) {
} }
// node.SetValue(x); x == node.GetValue // node.SetValue(x); x == node.GetValue
func TestElementNodeValueFetchAndSetIdentity (t *testing.T) { func TestElementNodeValueFetchAndSetIdentity(t *testing.T) {
// Setup: <root><text>xyzzy</text></root> // Setup: <root><text>xyzzy</text></root>
// The xmlx parser creates a nameless NT_TEXT node containing the value 'xyzzy' // The xmlx parser creates a nameless NT_TEXT node containing the value 'xyzzy'
rootN := NewNode(NT_ROOT) rootN := NewNode(NT_ROOT)
rootN.Name = xml.Name{Space: "", Local: "root"} rootN.Name = xml.Name{Space: "", Local: "root"}
textN := NewNode(NT_ELEMENT) textN := NewNode(NT_ELEMENT)
@ -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)
} }
@ -316,7 +316,7 @@ func TestElementNodeValueFetchAndSet(t *testing.T) {
if carN == nil { if carN == nil {
t.Fatalf("Failed to get the car, got nil, wanted Node{car}") t.Fatalf("Failed to get the car, got nil, wanted Node{car}")
} }
colorNode := carN.SelectNode("", "color") colorNode := carN.SelectNode("", "color")
if colorNode == nil { if colorNode == nil {
t.Fatalf("Failed to get the color, got nil, wanted Node{color}") t.Fatalf("Failed to get the color, got nil, wanted Node{color}")
@ -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))
}
}