本文介绍了创建没有结束标记的 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个嵌套的 golang 结构:

I have this nested golang struct:

// TierRequest is the outer most XML envelope of soap request
type TierRequest struct {
    XMLName   xml.Name `xml:"soapenv:Envelope"`
    NsEnv     string   `xml:"xmlns:soapenv,attr"`
    NsType    string   `xml:"xmlns:typ,attr"`
    Header    string   `xml:"soapenv:Header"`

// TierBody is an emtpy container with the GetCollectorProfile struct
type TierBody struct {
    GetCollectorProfiles GetCollectorProfile `Collectorxml:"typ:GetCollectorProfileRequest"`
}

// GetCollectorProfile struct has the context and collector number
type GetCollectorProfile struct {
    Contexts CollectorContext `xml:"typ:Context"`
    Number   int              `xml:"typ:CollectorNumber"`
}

// CollectorContext contanins a few variables as attributes
type CollectorContext struct {
    Channel  string `xml:"Channel,attr"`
    Source   string `xml:"Source,attr"`
    Language string `xml:"LanguageCode,attr"`
}

当我用值初始化它并用 encoding/xml 封送它时,它看起来像这样:

When I initialize it with values and marshal with encoding/xml it comes to look like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:typ="http:/www.yahoo.com/tp/ets/2008/04/01/collector/types">
  <soapenv:Header></soapenv:Header>
  <soapenv:Body>
    <GetCollectorProfiles>
      <typ:Context Channel="WEB" Source="WEB" LanguageCode="en-CA"></typ:Context>
      <typ:CollectorNumber>50000</typ:CollectorNumber>
    </GetCollectorProfiles>
  </soapenv:Body>
</soapenv:Envelope>

我怎样才能去掉 soapenv:Headertyp:Context 的结束标签,所以它看起来就像 <soapenv:Header/>?

How I can get rid of the closing tags for soapenv:Header and typ:Context, so it just looks like <soapenv:Header/>?

推荐答案

没有内容的 元素和 结束标签:

<soapenv:Header></soapenv:Header>

还有一个空元素标签:

<soapenv:Header/>

要控制使用哪种形式,您必须将数据视为文本而不是 XML,但最好不要担心没有区别的差异.

[为完整性添加]

...除了晦涩和过时的推荐

...other than an obscure and antiquated recommendation

为了互操作性,应该使用空元素标签,并且应该仅用于声明为 EMPTY 的元素.

关于与 SGML 的互操作性:

regarding interoperability with SGML:

为了互操作性

[定义:标记描述非约束性推荐的句子包括以增加处理 XML 文档的机会由早于 SGML 处理器的现有安装基础WebSGML 适应 ISO 8879 附录.]

[Definition: Marks a sentence describing a non-binding recommendation included to increase the chances that XML documents can be processed by the existing installed base of SGML processors which predate the WebSGML Adaptations Annex to ISO 8879.]

这篇关于创建没有结束标记的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:49