创建XML元素而不关闭标记

时间:2015-04-26 22:09:50

标签: xml go xml-parsing

我有这个嵌套的golang结构:

// 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对值进行初始化时,它看起来像这样:

<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/>

1 个答案:

答案 0 :(得分:3)

没有内容的 元素与end-tag 之间的XML级别没有区别:

<soapenv:Header></soapenv:Header>

empty element tag

<soapenv:Header/>

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

[为完整性添加]

......除了模糊和过时的recommendation

  

For interoperability,应该使用empty-element标签   仅用于声明为EMPTY的元素。

关于与SGML的互操作性:

  

实现互操作性

     

[定义:标记描述非约束性建议的句子   包括在内以增加处理XML文档的机会   通过现有的SGML处理器安装基础   WebSGML Adaptations ISO 8879的附件。]

相关问题