登录
首页 >  Golang >  Go问答

合并两个命名空间中具有相同属性名称的 XML 属性

来源:stackoverflow

时间:2024-02-27 12:27:25 166浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《合并两个命名空间中具有相同属性名称的 XML 属性》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我有一个 xml 结构,其中有一个未命名空间的“类型”属性和一个命名空间属性。我无法让 go 的解组器读取两个“类型”属性。

xml 是:

data := `


`

我的 go/xml 定义是:

type typemap struct {
    xmlname xml.name `xml:"map"`
    name    string   `xml:"name,attr"`
    type    string   `xml:"urn:debugger_protocol_v1 type,attr"`
    xsitype string   `xml:"http://www.w3.org/2001/xmlschema-instance type,attr"`
}

type response struct {
    xmlname xml.name `xml:"response"`
    typemap []typemap `xml:"map,omitempty"`
}

运行以下代码时:

package main

import (
    "encoding/xml"
    "fmt"
    "strings"
)

type typemap struct {
    xmlname xml.name `xml:"map"`
    name    string   `xml:"name,attr"`
    type    string   `xml:"urn:debugger_protocol_v1 type,attr"`
    xsitype string   `xml:"http://www.w3.org/2001/xmlschema-instance type,attr"`
}

type response struct {
    xmlname xml.name  `xml:"response"`
    typemap []typemap `xml:"map,omitempty"`
}

func main() {
    rq := new(response)
    data := `


`

    reader := strings.newreader(data)

    decoder := xml.newdecoder(reader)

    err := decoder.decode(&rq)

    if err != nil {
        fmt.println(err)
    }

    fmt.printf("unmarshalled content:\n%v", rq)
}

输出缺少 bool_t,如下所示:

&{{urn:debugger_protocol_v1 response} [
    {{urn:debugger_protocol_v1 map} bool  xsd:boolean}
    {{urn:debugger_protocol_v1 map} int  xsd:decimal}
]}

如果我从定义中删除 urn:debugger_protocol_v1,则会收到以下错误:

main.Typemap field "Type" with tag "type,attr" conflicts with field "XsiType" with tag "http://www.w3.org/2001/XMLSchema-instance type,attr"

我无法更改原始 xml 数据格式。有没有办法解组两个 type 属性?

我也设置了一个 go 演示


解决方案


事实证明,目前还没有办法在 go 中本地执行此操作。有一个 namespace related issues 的列表,以及一个描述它的 specific issue,但这尚未得到解决。

不过,我也找到了解决方法。您可以编写自己的解组代码,既可以用于 attributes,也可以用于 elements

对于属性,您可以实现如下内容:

type nonmarshalled string

func (s *nonmarshalled) unmarshalxmlattr(attr xml.attr) error {
    fmt.printf("parsing attribute '%s', with value '%s'", attr.name.local, attr.value)
    s.title = strings.toupper(attr.value)
    return nil
}

type typemap struct {
    xmlname xml.name `xml:"map"`
    name    string   `xml:"name,attr"`
    type    nonnamespaced `xml:"type,attr"`
    xsitype string   `xml:"http://www.w3.org/2001/xmlschema-instance type,attr"`
}

但是,这并不能同时拥有命名空间属性名称和具有相同名称的非命名空间属性。这仍然会导致:

main.typemap field "type" with tag "type,attr" conflicts with field "xsitype" with tag "http://www.w3.org/2001/xmlschema-instance type,attr"

这里的解决方法是为整个 typemap 类型编写一个解组器。就我而言,这段代码如下所示:

func (s *Typemap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {                                         
    nameAttr := ""                                                                                                     
    typeAttr := ""                                                                                                     
    xsiTypeAttr := ""                                                                                                  

    for _, attr := range start.Attr {                                                                                  
        if attr.Name.Space == "" && attr.Name.Local == "name" {                                                        
            nameAttr = attr.Value                                                                                      
        }                                                                                                              
        if attr.Name.Space == "" && attr.Name.Local == "type" {                                                        
            typeAttr = attr.Value                                                                                      
        }                                                                                                              
        if attr.Name.Space == "http://www.w3.org/2001/XMLSchema-instance" && attr.Name.Local == "type" {               
            xsiTypeAttr = attr.Value                                                                                   
        }                                                                                                              
    }                                                                                                                  

    d.Skip()                                                                                                           
    *s = Typemap{Name: nameAttr, Type: typeAttr, XsiType: xsiTypeAttr}                                                 

    return nil                                                                                                         
}

到这里,我们也就讲完了《合并两个命名空间中具有相同属性名称的 XML 属性》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>