创建没有闭合标签的XML元素。

huangapple go评论95阅读模式
英文:

Create XML element without closing tag

问题

我有这个嵌套的 Golang 结构体:

// TierRequest 是 SOAP 请求的最外层 XML 包裹
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 是一个空容器,包含 GetCollectorProfile 结构体
type TierBody struct {
    GetCollectorProfiles GetCollectorProfile `xml:"typ:GetCollectorProfileRequest"`
}

// GetCollectorProfile 结构体包含上下文和收集器编号
type GetCollectorProfile struct {
    Contexts CollectorContext `xml:"typ:Context"`
    Number   int              `xml:"typ:CollectorNumber"`
}

// CollectorContext 包含几个变量作为属性
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/>

英文:

I have this nested golang struct:

<!-- begin snippet: js hide: false -->

<!-- language: lang-go -->

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

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

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

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

<!-- end snippet -->

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

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

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

答案1

得分: 6

在XML级别上,没有内容的元素和带有end-tag的元素之间没有区别

&lt;soapenv:Header&gt;&lt;/soapenv:Header&gt;

和***empty element tag***:

&lt;soapenv:Header/&gt;

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


[为了完整性而添加]

...除了一个晦涩而过时的建议

> 为了互操作性,应该使用空元素标签,并且只能用于声明为空的元素。

关于与SGML的互操作性:

> 为了互操作性
>
> [定义:标记一个句子,描述了一个非约束性的建议,旨在增加XML文档能够被现有的在WebSGML Adaptations Annex to ISO 8879之前就存在的SGML处理器处理的机会。]

英文:

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

&lt;soapenv:Header&gt;&lt;/soapenv:Header&gt;

and an empty element tag:

&lt;soapenv:Header/&gt;

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

huangapple
  • 本文由 发表于 2015年4月27日 06:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/29884177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定