英文:
Get XML namespace prefix in Go using Unmarshal
问题
我想知道是否可以使用encoding/xml
中的Unmarshal
方法来获取XML命名空间前缀。
例如,我有以下代码:
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema">
</application>
我想知道如何检索定义XMLSchema前缀的xs
,而不必使用Token
方法。
英文:
I would like to know if it is possible to get the XML namespace prefix using the
Unmarshal
method in encoding/xml
.
For example, I have:
<application xmlns="http://wadl.dev.java.net/2009/02" xmlns:xs="http://www.w3.org/2001/XMLSchema">
</application>
I would like to know how to retrieve the xs
defining the prefix for XMLSchema, without having to use the Token
method.
答案1
得分: 3
只需像处理其他属性一样获取它:
type App struct {
XS string `xml:"xs,attr"`
}
Playground: http://play.golang.org/p/2IOmkX1Jov.
如果你还有一个实际的 xs
属性,没有 xmlns
,那就会变得更加棘手。即使你将命名空间 URI 添加到 XS
的标签中,你可能会得到一个错误。
编辑: 如果你想获取所有声明的命名空间,你可以在你的元素上定义一个自定义的 UnmarshalXML
方法并扫描它的属性:
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{}
for _, attr := range start.Attr {
if attr.Name.Space == "xmlns" {
a.Namespaces[attr.Name.Local] = attr.Value
}
}
// 继续解组。
type app App
aa := (*app)(a)
return d.DecodeElement(aa, &start)
}
Playground: http://play.golang.org/p/u4RJBG3_jW.
英文:
Just get it like every other attribute:
type App struct {
XS string `xml:"xs,attr"`
}
Playground: http://play.golang.org/p/2IOmkX1Jov.
It gets trickier if you also have an actual xs
attribute, sans xmlns
. Even if you add the namespace URI to XS
's tag, you will probably get an error.
EDIT: If you want to get all declared namespaces, you can define a custom UnmarshalXML
on your element and 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{}
for _, attr := range start.Attr {
if attr.Name.Space == "xmlns" {
a.Namespaces[attr.Name.Local] = attr.Value
}
}
// Go on with unmarshalling.
type app App
aa := (*app)(a)
return d.DecodeElement(aa, &start)
}
Playground: http://play.golang.org/p/u4RJBG3_jW.
答案2
得分: 0
目前(Go 1.5),似乎不可能实现。
我找到的唯一解决方案是重新定位元素:
func NewDocument(r io.ReadSeeker) (*Document, error) {
decoder := xml.NewDecoder(r)
// 首先获取xml命名空间
rootToken, err := decoder.Token()
if err != nil {
return nil, err
}
var xmlSchemaNamespace string
switch element := rootToken.(type) {
case xml.StartElement:
for _, attr := range element.Attr {
if attr.Value == xsd.XMLSchemaURI {
xmlSchemaNamespace = attr.Name.Local
break
}
}
}
/* 处理命名空间 */
// 重新定位
r.Seek(0, 0)
// 标准解码
decoder = xml.NewDecoder(r)
err = decoder.Decode(&w)
/* ... */
}
希望对你有帮助!
英文:
Currently (Go 1.5), it does not seems to be possible.
The only solution I found, was to use rewind the element:
func NewDocument(r io.ReadSeeker) (*Document, error) {
decoder := xml.NewDecoder(r)
// Retrieve xml namespace first
rootToken, err := decoder.Token()
if err != nil {
return nil, err
}
var xmlSchemaNamespace string
switch element := rootToken.(type) {
case xml.StartElement:
for _, attr := range element.Attr {
if attr.Value == xsd.XMLSchemaURI {
xmlSchemaNamespace = attr.Name.Local
break
}
}
}
/* Process name space */
// Rewind
r.Seek(0, 0)
// Standart unmarshall
decoder = xml.NewDecoder(r)
err = decoder.Decode(&w)
/* ... */
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论