为什么在Marshaller中不使用嵌入式结构类型的XMLName值?

问题描述 投票:0回答:1

许多肥皂服务使用unqualified attribute/element namespace表格

<schema id=ID attributeFormDefault=qualified|unqualified elementFormDefault=qualified|unqualified ...
The form for elements declared in the target namespace of this schema. The value must be "qualified" or "unqualified". Default is "unqualified". "unqualified" indicates that elements from the target namespace are not required to be qualified with the namespace prefix. "qualified" indicates that elements from the target namespace must be qualified with the namespace prefix

我需要为此服务创建客户端,所以我对其进行测试

type Test struct {
  XMLName xml.Name
  XMLNs   string `xml:"xmlns:c,attr"`
  ID      int
}

func main() {
  var t1 Test
  t1.XMLName.Local = "c:a"
  t1.XMLNs = "sample"

  d1, _ := xml.Marshal(&t1)
  fmt.Println(string(d1))
}

it works as it should。但是,当我尝试使用XMLName创建通用的嵌入式类型时

type Test2 struct {
  XMLForm
  ID int
}

type XMLForm struct {
  XMLName xml.Name //`xml:"c:<name>"`
  XMLNs   string   `xml:"xmlns:c,attr,omitempty"`
}

func (f *XMLForm) setXMLUnqualifiedName(name string) {
  f.XMLName.Local = "c:" + name
  f.XMLNs = "sample"
}

func main() {
  var t2 Test2
  t2.setXMLUnqualifiedName("a")

  d2, _ := xml.Marshal(t2)
  fmt.Println(string(d2))
}

失败on go1.14.1\src\encoding\xml\marshal.go

482: } else if v, ok := xmlname.value(val).Interface().(Name); ok && v.Local != "" {
483:       start.Name = v
484: }

(无法转换为xml.Name)。是虫子吗?还是反映出局限性?尽管如此,标记仍有效

我不是围棋专家,我对优先顺序感到困惑:

  • XMLName字段上的标记,如果数据是结构,则>]
  • Name类型的XMLName字段的值
  • 用于获取数据的struct字段的标签
  • 用于获取数据的结构字段的名称
  • 封送处理类型的名称
  • 标签不能在运行时更改。为什么首选标签而不是值?

许多肥皂服务使用不合格的属性/元素名称空间形式

xml go
1个回答
0
投票

(无法转换为xml.Name)。是虫子吗?还是反映出局限性?

© www.soinside.com 2019 - 2024. All rights reserved.