@@ -2,6 +2,7 @@ package xpath
22
33import (
44 "bytes"
5+ "fmt"
56 "strings"
67 "testing"
78)
@@ -29,6 +30,60 @@ func TestCompile(t *testing.T) {
2930 if err != nil {
3031 t .Fatalf ("/a/b/(c, .[not(c)]) should be correct but got error %s" , err )
3132 }
33+ _ , err = Compile ("/pre:foo" )
34+ if err != nil {
35+ t .Fatalf ("/pre:foo should be correct but got error %s" , err )
36+ }
37+ }
38+
39+ func TestCompileWithNS (t * testing.T ) {
40+ _ , err := CompileWithNS ("/foo" , nil )
41+ if err != nil {
42+ t .Fatalf ("/foo {nil} should be correct but got error %s" , err )
43+ }
44+ _ , err = CompileWithNS ("/foo" , map [string ]string {})
45+ if err != nil {
46+ t .Fatalf ("/foo {} should be correct but got error %s" , err )
47+ }
48+ _ , err = CompileWithNS ("/foo" , map [string ]string {"a" : "b" })
49+ if err != nil {
50+ t .Fatalf ("/foo {a:b} should be correct but got error %s" , err )
51+ }
52+ _ , err = CompileWithNS ("/a:foo" , map [string ]string {"a" : "b" })
53+ if err != nil {
54+ t .Fatalf ("/a:foo should be correct but got error %s" , err )
55+ }
56+ _ , err = CompileWithNS ("/u:foo" , map [string ]string {"a" : "b" })
57+ msg := fmt .Sprintf ("%v" , err )
58+ if msg != "prefix u not defined." {
59+ t .Fatalf ("expected 'prefix u not defined' but got: %s" , msg )
60+ }
61+ }
62+
63+ func TestNamespace (t * testing.T ) {
64+ doc := createNode ("" , RootNode )
65+ books := doc .createChildNode ("books" , ElementNode )
66+ book1 := books .createChildNode ("book" , ElementNode )
67+ book1 .createChildNode ("book1" , TextNode )
68+ book2 := books .createChildNode ("b:book" , ElementNode )
69+ book2 .addAttribute ("xmlns:b" , "ns" )
70+ book2 .createChildNode ("book2" , TextNode )
71+ book3 := books .createChildNode ("c:book" , ElementNode )
72+ book3 .addAttribute ("xmlns:c" , "ns" )
73+ book3 .createChildNode ("book3" , TextNode )
74+
75+ // Existing behaviour:
76+ v := joinValues (selectNodes (doc , "//b:book" ))
77+ if v != "book2" {
78+ t .Fatalf ("expected book2 but got %s" , v )
79+ }
80+
81+ // With namespace bindings:
82+ exp , _ := CompileWithNS ("//x:book" , map [string ]string {"x" : "ns" })
83+ v = joinValues (iterateNodes (exp .Select (createNavigator (doc ))))
84+ if v != "book2,book3" {
85+ t .Fatalf ("expected 'book2,book3' but got %s" , v )
86+ }
3287}
3388
3489func TestMustCompile (t * testing.T ) {
@@ -464,6 +519,14 @@ func selectNodes(root *TNode, expr string) []*TNode {
464519 return iterateNodes (t )
465520}
466521
522+ func joinValues (nodes []* TNode ) string {
523+ s := make ([]string , 0 )
524+ for _ , n := range nodes {
525+ s = append (s , n .Value ())
526+ }
527+ return strings .Join (s , "," )
528+ }
529+
467530func createNavigator (n * TNode ) * TNodeNavigator {
468531 return & TNodeNavigator {curr : n , root : n , attr : - 1 }
469532}
@@ -516,10 +579,28 @@ func (n *TNodeNavigator) LocalName() string {
516579 if n .attr != - 1 {
517580 return n .curr .Attr [n .attr ].Key
518581 }
519- return n .curr .Data
582+ name := n .curr .Data
583+ if strings .Contains (name , ":" ) {
584+ return strings .Split (name , ":" )[1 ]
585+ }
586+ return name
520587}
521588
522589func (n * TNodeNavigator ) Prefix () string {
590+ if n .attr == - 1 && strings .Contains (n .curr .Data , ":" ) {
591+ return strings .Split (n .curr .Data , ":" )[0 ]
592+ }
593+ return ""
594+ }
595+
596+ func (n * TNodeNavigator ) NamespaceURL () string {
597+ if n .Prefix () != "" {
598+ for _ , a := range n .curr .Attr {
599+ if a .Key == "xmlns:" + n .Prefix () {
600+ return a .Value
601+ }
602+ }
603+ }
523604 return ""
524605}
525606
0 commit comments