Golang: how to unmarshal XML attributes with colons?

大憨熊 提交于 2019-12-20 10:59:15

问题


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.


回答1:


Your example fragment is not quite correct, since it does not include an XML namespace binding for the xlink: prefix. What you probably want is:

<g xmlns:xlink="http://www.w3.org/1999/xlink">
  <a xlink:href="http://example.com" data-bind="121">lala</a>
</g>

You can unmarshal this attribute using the namespace URL:

XlinkHref string `xml:"http://www.w3.org/1999/xlink href,attr"`

Here is an updated copy of your example program with the namespace fix.



来源:https://stackoverflow.com/questions/18934327/golang-how-to-unmarshal-xml-attributes-with-colons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!