dourunlao1642 2015-04-26 22:09
浏览 247
已采纳

创建XML元素而不关闭标签

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"`
}

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>

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

  • 写回答

1条回答 默认 最新

  • duanmeng2842 2015-04-26 22:43
    关注

    There is no difference at the XML level between an element with no content and an end-tag:

    <soapenv:Header></soapenv:Header>
    

    and an empty element tag:

    <soapenv:Header/>
    

    To control which form is used, you'd have to treat the data as text rather than as XML, but better not to worry about a difference that makes no difference.


    [Added for completeness]

    ...other than an obscure and antiquated recommendation

    For interoperability, the empty-element tag should be used, and should only be used, for elements which are declared EMPTY.

    regarding interoperability with SGML:

    for interoperability

    [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.]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料