Merge pull request #13 from ThomsonReutersEikon/master

fix: String() handled ns prefixes incorrectly
This commit is contained in:
jimt 2013-09-03 17:21:14 -07:00
commit 662d1a3eaa
3 changed files with 47 additions and 1 deletions

17
node.go
View File

@ -500,7 +500,8 @@ func (this *Node) printElement(indent int) []byte {
for _, v := range this.Attributes {
if len(v.Name.Space) > 0 {
b.WriteString(fmt.Sprintf(` %s:%s="%s"`, v.Name.Space, v.Name.Local, v.Value))
prefix := this.spacePrefix(v.Name.Space)
b.WriteString(fmt.Sprintf(` %s:%s="%s"`, prefix, v.Name.Local, v.Value))
} else {
b.WriteString(fmt.Sprintf(` %s="%s"`, v.Name.Local, v.Value))
}
@ -541,6 +542,20 @@ func (this *Node) printElement(indent int) []byte {
return b.Bytes()
}
// spacePrefix resolves the given space (e.g. a url) to the prefix it was
// assigned by an attribute by the current node, or one of its parents.
func (this *Node) spacePrefix(space string) string {
for _, attr := range this.Attributes {
if attr.Name.Space == "xmlns" && attr.Value == space {
return attr.Name.Local
}
}
if this.Parent == nil {
return space
}
return this.Parent.spacePrefix(space)
}
// Add a child node
func (this *Node) AddChild(t *Node) {
if t.Parent != nil {

8
test3.xml Normal file
View File

@ -0,0 +1,8 @@
<root xmlns:foo='http:/example.org/foo'>
<child foo:bar='1'>
<grandchild xmlns:foo=''>
<great-grandchild foo:bar='2'>
</great-grandchild>
</grandchild>
</child>
</root>

View File

@ -136,3 +136,26 @@ func TestUnmarshal(t *testing.T) {
return
}
}
func TestString(t *testing.T) {
doc := New()
err := doc.LoadFile("test3.xml", nil)
if err != nil {
t.Errorf("LoadFile(): %s", err)
return
}
expected := `<root xmlns:foo="http:/example.org/foo">
<child foo:bar="1">
<grandchild xmlns:foo="">
<great-grandchild bar="2" />
</grandchild>
</child>
</root>
`
if got := doc.Root.String(); got != expected {
t.Fatalf("expected: %s\ngot: %s\n", expected, got)
}
}