为什么在 XML 中使用空元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29082189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:07:18  来源:igfitidea点击:

Why use an empty element in XML?

xml

提问by Steve T.

Rank amateur here. I get the instructions on HOW to use an empty element in XML. But why would you? What would it be used for, other than (I guess) to add a blank record when the XML is parsed?

在这里排名业余。我得到了关于如何在 XML 中使用空元素的说明。但你为什么要这样做?除了(我猜)在解析 XML 时添加空白记录之外,它还有什么用途?

Here's information from W3Schools:

以下是来自 W3Schools 的信息:

"Empty XML Elements An element with no content is said to be empty. In XML, you can indicate an empty element like this: <element></element>or you can use an empty tag, like this (this sort of element syntax is called self-closing): <element />The two forms above produce identical results in an XML parser."

”Empty XML Elements 没有内容的元素被称为空元素。在 XML 中,您可以像这样指示一个空元素:<element></element>或者您可以像这样使用一个空标记(这种元素语法称为自闭):<element />上述两种形式在 XML 解析器中产生相同的结果。”

回答by Dan Field

If a schema requires that a tag be present in a certain order, you may need to include that tag even if it has an empty value. For example, the following fragment from a schema indicates you need tag foo before tag bar:

如果模式要求标签以特定顺序出现,您可能需要包含该标签,即使它有一个空值。例如,模式中的以下片段表明您需要在标签栏之前使用标签 foo:

<xs:element name="parent">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="foo" minOccurs="1" maxOccurs="1" />
      <xs:element name="bar" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

so the following fragment would be valid:

所以以下片段将是有效的:

<parent>
  <foo />
  <bar>asdf!</bar>
</parent>

But the following would not:

但以下不会:

<parent>
  <bar>asdf!</bar>
</parent>

Additionally, you can use an empty tag to represent an empty value (as opposed to a null value). However, if you're trying to represent a null value in XML, you should really use an xsi:nilattribute

此外,您可以使用空标签来表示空值(而不是空值)。但是,如果您尝试在 XML 中表示空值,则应该真正使用xsi:nil属性

回答by CodeGodie

From my experience, Ive come across many of these formats when using APIs. Sometimes, the API creator has variables that are empty, thus resulting in these empty XML tags. For example, let's say you are creating a website that gathers hotel availability for different hotels. Company A ( some hotel company ) provides you with access to their API. You send a request and they provide you with the following:

根据我的经验,我在使用 API 时遇到过许多这些格式。有时,API 创建者的变量是空的,从而导致这些空的 XML 标记。例如,假设您正在创建一个收集不同酒店的酒店可用性的网站。公司 A(一些酒店公司)为您提供访问其 API 的权限。您发送请求,他们为您提供以下信息:

<response>
  <date>2008-01-10</date>
  <available>TRUE</available>
</response>

But Sometimes you may get:

但有时你可能会得到:

<response>
  <date>2008-01-10</date>
  <available>FALSE</available>
</response>

Or sometimes you may get:

或者有时你可能会得到:

<response>
  <date>2008-01-10</date>
  <available></available>
</response>

As a developer, you have to create code that is ready for any type of response since we dont know exactly how the data is sent back from the API creators, which is why this is mentioned in tutorials. Hope this helps.

作为开发人员,您必须创建可用于任何类型响应的代码,因为我们不确切知道数据是如何从 API 创建者发回的,这就是教程中提到的原因。希望这可以帮助。

回答by harpo

I think you are using "empty" elements to mean "elements with no children of any kind." Self-closing elements -- ones which don't have a separate closing tag -- may still have attributes and aren't empty in that sense.

我认为您使用“空”元素来表示“没有任何子元素的元素”。自闭合元素——没有单独的结束标记的元素——可能仍然具有属性并且在这个意义上不是空的。

(X)HTML itself defines "empty" elements, such as <br/>. Again, even <br/>may have classand other attributes.

(X)HTML 本身定义了“空”元素,例如<br/>. 同样,甚至<br/>可能具有class和其他属性。

To the question of how they might be used in your own schemas, you can use empty XML elements as "markers" (borrowing from the way that .NET uses the term "marker interface" for an interface with no members). This is useful when you want to add a "boolean" attribute to an element (like HTML allows valueless attributes such as checked). In such cases, using an attribute would force you to add noise of some kind (in the form of a meaningless or empty value).

对于如何在您自己的模式中使用它们的问题,您可以使用空的 XML 元素作为“标记”(借用 .NET 对没有成员的接口使用术语“标记接口”的方式)。当您想向元素添加“布尔值”属性时,这很有用(如 HTML 允许无值属性,例如checked)。在这种情况下,使用属性会迫使您添加某种噪音(以无意义或空值的形式)。

I use it this way, for example, to mark speeches as prose in Shakespeare plays:

例如,我用这种方式将演讲标记为莎士比亚戏剧中的散文:

<speech>
    <speaker role="Archidamus">Archidamus</speaker>
    <prose/>
    <line a="If_you">If you shall chance, Camillo, to visit Bohemia, on</line>
    <line a="the_like">the like occasion whereon my services are now on</line>
    <line a="foot_you">foot, you shall see, as I have said, great</line>
    <line a="difference_betwixt">difference betwixt our Bohemia and your Sicilia.</line>
</speech>

That said, it is kind of a kludge to work around what can be seen as a defect of XML, viz, the inability to represent non-text primitives.

也就是说,解决可被视为 XML 的缺陷,无法表示非文本原语的问题有点麻烦。