I sent a patch for the simple (not entirely correct) thing, as you saw. I won't be offended if it's rejected; I'm more concerned about the "correct" solution. To that end, here's a proposed API and example tests. A new field would be recognized in any marshalable struct, named XMLNs of type xml.Ns:
// An Ns represents an xmlns prefix-to-namespace mapping.
type Ns struct {
Prefix, Uri string
}
The example from
http://www.w3schools.com/xml/xml_namespaces.asp involves HTML and XML intermixed, with an HTML table and a custom "furniture table" element in the same document. Here are illustrative struct types and test cases. Note that these test cases use values for that field rather than tags on the field; I assume we'd want to support both.
type NsRoot struct {
XMLName Name `xml:"root"`
XMLNs []Ns
HTable HtmlTable `xml:"
http://www.w3.org/TR/html4/ table"`
FTable FurnTable `xml:"
http://www.w3schools.com/furniture table"`
}
type HtmlTable struct {
XMLName Name `xml:"
http://www.w3.org/TR/html4/ table"`
XMLNs []Ns
Rows []HtmlTr `xml:"
http://www.w3.org/TR/html4/ tr"`
}
type HtmlTr struct {
XMLName Name `xml:"
http://www.w3.org/TR/html4/ tr"`
Td []string `xml:"
http://www.w3.org/TR/html4/ td"`
}
type FurnTable struct {
XMLName Name `xml:"
http://www.w3schools.com/furniture table"`
XMLNs []Ns
Name string
Width int
Length int
}
{
ExpectXML: `<root xmlns:h="
http://www.w3.org/TR/html4/"` +
` xmlns:f="
http://www.w3schools.com/furniture">` +
`<h:table>`+
`<h:tr>` +
`<h:td>Apples</h:td>` +
`<h:td>Bananas</h:td>` +
`</h:tr>` +
`</h:table>` +
`<f:table>` +
`<f:name>African Coffee Table</f:name>` +
`<f:width>80</f:width>` +
`<f:length>120</f:length>` +
`</f:table>` +
`</root>`,
Value: &NsRoot{XMLNs: []Ns{Ns{Prefix: "h",
Uri: "
http://www.w3.org/TR/html4/"},
Ns{Prefix: "f",
Uri: "
http://www.w3schools.com/furniture"}},
HTable: HtmlTable{Rows: []HtmlTr{HtmlTr{Td:
[]string{"Apples", "Bananas"}}}},
FTable: FurnTable{Name: "African Coffee Table",
Width: 80, Length: 120},
},
},
{
ExpectXML: `<root>` +
`<h:table xmlns:h="
http://www.w3.org/TR/html4/">` +
`<h:tr>` +
`<h:td>Apples</h:td>` +
`<h:td>Bananas</h:td>` +
`</h:tr>` +
`</h:table>` +
`<f:table xmlns:f="
http://www.w3schools.com/furniture">` +
`<f:name>African Coffee Table</f:name>` +
`<f:width>80</f:width>` +
`<f:length>120</f:length>` +
`</f:table>` +
`</root>`,
Value: &NsRoot{HTable: HtmlTable{XMLNs: []Ns{Ns{Prefix: "h",
Uri: "
http://www.w3.org/TR/html4/"}},
Rows: []HtmlTr{HtmlTr{Td:
[]string{"Apples", "Bananas"}}}},
FTable: FurnTable{XMLNs: []Ns{Ns{Prefix: "f",
Uri: "
http://www.w3schools.com/furniture"}},
Name: "African Coffee Table",
Width: 80, Length: 120},
},
},
{
ExpectXML: `<root>` +
`<table xmlns="
http://www.w3.org/TR/html4/">` +
`<tr>` +
`<td>Apples</td>` +
`<td>Bananas</td>` +
`</tr>` +
`</table>` +
`<table xmlns="
http://www.w3schools.com/furniture">` +
`<name>African Coffee Table</name>` +
`<width>80</width>` +
`<length>120</length>` +
`</table>` +
`</root>`,
Value: &NsRoot{HTable: HtmlTable{Rows: []HtmlTr{HtmlTr{Td:
[]string{"Apples", "Bananas"}}}},
FTable: FurnTable{Name: "African Coffee Table",
Width: 80, Length: 120},
},
},