diff --git a/node.go b/node.go
index 85e4542..18fb9ee 100644
--- a/node.go
+++ b/node.go
@@ -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 {
diff --git a/test3.xml b/test3.xml
new file mode 100644
index 0000000..77f50f5
--- /dev/null
+++ b/test3.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
diff --git a/xmlx_test.go b/xmlx_test.go
index 97fe4de..44a2ac8 100644
--- a/xmlx_test.go
+++ b/xmlx_test.go
@@ -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 := `
+
+
+
+
+
+
+`
+
+ if got := doc.Root.String(); got != expected {
+ t.Fatalf("expected: %s\ngot: %s\n", expected, got)
+ }
+}