diff --git a/query.go b/query.go index 276ee71..fd05108 100644 --- a/query.go +++ b/query.go @@ -53,11 +53,13 @@ func getCurrentNode(it *xpath.NodeIterator) *Node { Data: n.Value(), } return &Node{ - Parent: n.curr, - Type: AttributeNode, - Data: n.LocalName(), - FirstChild: childNode, - LastChild: childNode, + Parent: n.curr, + Type: AttributeNode, + Data: n.LocalName(), + Prefix: n.Prefix(), + NamespaceURI: n.NamespaceURL(), + FirstChild: childNode, + LastChild: childNode, } } return n.curr diff --git a/query_test.go b/query_test.go index b4158be..1793851 100644 --- a/query_test.go +++ b/query_test.go @@ -44,6 +44,27 @@ const xmlDoc = ` var doc = loadXML(xmlDoc) +func TestQueryAttributeNamespace(t *testing.T) { + root, err := Parse(strings.NewReader(``)) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + expr, prefix, namespace, data, value string + }{ + {"//@xlink:href", "xlink", "http://www.w3.org/1999/xlink", "href", "target"}, + {"//@id", "", "", "id", "plain"}, + } { + node, err := Query(root, tc.expr) + if err != nil || node == nil { + t.Fatalf("Query(%q) = %v, %v", tc.expr, node, err) + } + if node.Prefix != tc.prefix || node.NamespaceURI != tc.namespace || node.Data != tc.data || node.InnerText() != tc.value { + t.Errorf("Query(%q) = prefix %q, namespace %q, data %q, value %q", tc.expr, node.Prefix, node.NamespaceURI, node.Data, node.InnerText()) + } + } +} + func TestXPath(t *testing.T) { if list := Find(doc, "//book"); len(list) != 3 { t.Fatal("count(//book) != 3")