Get XML namespace prefix in Go using Unmarshal -
i know if possible xml namespace prefix using unmarshal method in encoding/xml.
for example, have:
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/xmlschema"> </application> i know how retrieve xs defining prefix xmlschema, without having use token method.
just every other attribute:
type app struct { xs string `xml:"xs,attr"` } playground: http://play.golang.org/p/2iomkx1jov.
it gets trickier if have actual xs attribute, sans xmlns. if add namespace uri xs's tag, get error.
edit: if want all declared namespaces, can define custom unmarshalxml on element , scan it's attributes:
type app struct { namespaces map[string]string foo int `xml:"foo"` } func (a *app) unmarshalxml(d *xml.decoder, start xml.startelement) error { a.namespaces = map[string]string{} _, attr := range start.attr { if attr.name.space == "xmlns" { a.namespaces[attr.name.local] = attr.value } } // go on unmarshalling. type app app aa := (*app)(a) return d.decodeelement(aa, &start) } playground: http://play.golang.org/p/u4rjbg3_jw.
Comments
Post a Comment