Allow SelectNode() calls with a wildcard as the namespace name ("*"). This allows us to find nodes if we do not strictly care about what namespace it carries. Same deal for attributes. node.SelectNode("*", foo") finds any node named "foo", regardless of what namespace it has.

This commit is contained in:
jim teeuwen 2011-02-01 15:29:35 +01:00
parent c07619da34
commit 5fff1fb51a
2 changed files with 10 additions and 7 deletions

13
node.go
View File

@ -155,7 +155,7 @@ func (this *Node) B(namespace, name string) bool {
// Get attribute value as string // Get attribute value as string
func (this *Node) As(namespace, name string) string { func (this *Node) As(namespace, name string) string {
for _, v := range this.Attributes { for _, v := range this.Attributes {
if namespace == v.Name.Space && name == v.Name.Local { if (namespace == "*" || namespace == v.Name.Space) && name == v.Name.Local {
return v.Value return v.Value
} }
} }
@ -251,11 +251,10 @@ func (this *Node) Ab(namespace, name string) bool {
return false return false
} }
// Returns true if this node has the specified attribute. False otherwise. // Returns true if this node has the specified attribute. False otherwise.
func (this *Node) HasAttr(namespace, name string) bool { func (this *Node) HasAttr(namespace, name string) bool {
for _, v := range this.Attributes { for _, v := range this.Attributes {
if namespace == v.Name.Space && name == v.Name.Local { if (namespace == "*" || namespace == v.Name.Space) && name == v.Name.Local {
return true return true
} }
} }
@ -268,7 +267,9 @@ func (this *Node) SelectNode(namespace, name string) *Node {
} }
func rec_SelectNode(cn *Node, namespace, name string) *Node { func rec_SelectNode(cn *Node, namespace, name string) *Node {
if cn.Name.Space == namespace && cn.Name.Local == name { // Allow wildcard for namespace names. Meaning we will match any namespace
// name with a matching local name.
if (namespace == "*" || cn.Name.Space == namespace) && cn.Name.Local == name {
return cn return cn
} }
@ -289,7 +290,9 @@ func (this *Node) SelectNodes(namespace, name string) []*Node {
} }
func rec_SelectNodes(cn *Node, namespace, name string, list *[]*Node) { func rec_SelectNodes(cn *Node, namespace, name string, list *[]*Node) {
if cn.Name.Space == namespace && cn.Name.Local == name { // Allow wildcard for namespace names. Meaning we will match any namespace
// name with a matching local name.
if (namespace == "*" || cn.Name.Space == namespace) && cn.Name.Local == name {
*list = append(*list, cn) *list = append(*list, cn)
return return
} }