如何去
问题描述:
编组XML时保持标签的命名空间URL时,我解组和元帅此XML的命名空间URL消失:如何去
<root xmlns:urn="http://test.example.com">
<urn:copyright>tekst</urn:copyright>
</root>
变为:
<root xmlns:urn="">
<urn:copyright></urn:copyright>
</root>
的代码:
package main
import (
"encoding/xml"
"fmt"
)
type Root struct {
XMLName xml.Name `xml:"root"`
XmlNS string `xml:"xmlns:urn,attr"`
Copyright Copyright `xml:"urn:copyright,omitempty"`
}
type Copyright struct {
Text string `xml:",chardata"`
}
func main() {
root := Root{}
x := `<root xmlns:urn="http://test.example.com">
<urn:copyright>text</urn:copyright>
</root>`
_ = xml.Unmarshal([]byte(x), &root)
b, _ := xml.MarshalIndent(root, "", " ")
fmt.Println(string(b))
}
答
Root.XmlNS未解组。
"The prefix xmlns is used only to declare namespace bindings and is by definition bound to the namespace name http://www.w3.org/2000/xmlns/."
https://www.w3.org/TR/REC-xml-names/
根据这两个XML规范和围棋拆封规则xml:"http://www.w3.org/2000/xmlns/ urn,attr"
应该还没有工作,它没有。我不认为维护者会喜欢后面的编组复杂性。
您可能想要执行以下操作。
type Root struct {
XMLName xml.Name `xml:"root"`
Copyright Copyright `xml:"http://test.example.com copyright"`
}
https://github.com/golang/go/issues/9519 –