Some SVG/XML files I'm working with have dashes and colons in attribute names - for example:
<g>
<a xlink:href="http://example.com" data-bind="121">...</a>
</g>
I'm trying to figure out how to unmarshal these attributes using golang
's encoding/xml
package. While the dashed attributes works, the ones with the colon doesn't:
[See here for a live example]
package main
import (
"encoding/xml"
"fmt"
)
var data = `
<g>
<a xlink:href="http://example.com" data-bind="121">lala</a>
</g>
`
type Anchor struct {
DataBind int `xml:"data-bind,attr"` // this works
XlinkHref string `xml:"xlink:href,attr"` // this fails
}
type Group struct {
A Anchor `xml:"a"`
}
func main() {
group := Group{}
_ = xml.Unmarshal([]byte(data), &group)
fmt.Printf("%#v\n", group.A)
}
These are seemingly legal attribute names; any idea how to extract the xlink:href
one? thanks.
xml:lang="eng"
, but seems like since xml is default(?) you can just use`xml:"lang,attr"`
as usual. – Igniter