Sebastian J
Sebastian J

Reputation: 325

Create XML element without closing tag

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

Upvotes: 2

Views: 3208

Answers (1)

kjhughes
kjhughes

Reputation: 111630

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

Upvotes: 6

Related Questions