XML token does not respect root element namespace prefix definition

Here is the XML structure:

<root xmlns:test="http://test.com/testns"> <test:sub> <title>this is title</title> </test:sub> </root> 

It gets unmarshalled with the structures defined below:

 type Root struct { XMLName xml.Name `xml:"root"` Sub *Sub } type Sub struct { XMLName xml.Name `xml:"http://test.com/testns sub"` Title string `xml:"title"` } 

This is what is taken back:

 <root> <sub xmlns="http://test.com/testns"> <title>this is title</title> </sub> </root> 

The definition of the root namespace prefix is ​​deleted after the marshal and auxiliary element use the url namespace instead of the prefix. Here is the code

Is there a way that marshal / unmarshal will not alter the xml structure? thanks!

+6
source share
1 answer

It does not seem to have changed the logical structure. In the original input, the root element declares the test prefix for the http://test.com/testns namespace, but does not actually declare itself in this namespace.

Here's an alternate version that does what you like: https://play.golang.org/p/NqNyIyMB4IP

I ran into a namespace before the root structure and added the test: prefix to the root xml element in the input.

0
source

All Articles