Html XML 文件中的自闭合标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7231902/
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-08-29 10:20:31  来源:igfitidea点击:

Self-closing tags in XML files

htmlxmltags

提问by Natasha

<tag id="foo" />

<tag id="foo" />

I noticed that they work with PHP SimpleXML.

我注意到他们使用 PHP SimpleXML。

But all XML examples I found on the web close them the old way:

但是我在网上找到的所有 XML 示例都以旧方式关闭它们:

<tag id="foo"></tag>

<tag id="foo"></tag>

Is there any reason why I should use the old method?

我有什么理由应该使用旧方法吗?

回答by Gordon

See the W3C specs for XML and XHTML:

请参阅 XML 和 XHTML 的 W3C 规范:

It depends on the Element Type declaration

这取决于元素类型声明

An element with no content is said to be empty. The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag.

没有内容的元素称为空元素。空元素的表示要么是紧跟在结束标签之后的开始标签,要么是空元素标签。

but also

但是也

Empty-element tags may be used for any element which has no content, whether or not it is declared using the keyword EMPTY. For interoperability, the empty-element tag SHOULD be used, and SHOULD only be used, for elements which are declared EMPTY.

空元素标签可用于任何没有内容的元素,无论是否使用关键字 EMPTY 声明。为了互操作性,对于声明为 EMPTY 的元素,应该使用并且应该只使用空元素标签。

This means, when your DTD contains something like

这意味着,当您的 DTD 包含类似

<!ELEMENT img EMPTY>

you shoulduse

应该使用

<img/>

unless you have good reason to use

除非你有充分的理由使用

<img></img>

Note that SHOULD is defined in RFC2119as

请注意,应该在 RFC2119定义

This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.

这个词,或形容词“推荐”,意味着在特定情况下可能存在合理的理由忽略特定项目,但在选择不同的课程之前必须理解并仔细权衡全部含义。

If you are working with XML that does not have a DTD or Schema, you can also influence how the XML is serialized with a predefined libxml constant:

如果您正在使用没有 DTD 或架构的 XML,您还可以使用预定义的 libxml 常量影响 XML 的序列化方式:

LIBXML_NOEMPTYTAG(integer): Expand empty tags (e.g. <br/>to <br></br>)

LIBXML_NOEMPTYTAG(整数):展开空标签(例如<br/><br></br>

But note that this option is currently just available in the functions DOMDocument::saveand DOMDocument::saveXML, so you cannot use it with SimpleXml.

但请注意,此选项目前仅在函数DOMDocument::saveand 中可用DOMDocument::saveXML,因此您不能将它与 SimpleXml 一起使用。

回答by bvdb

Yes, it's not as simple as it seems at first.

是的,这并不像起初看起来那么简单。

XSD differences:

XSD 差异:

First of all, it depends on your data type. If you use an XSD schema definition, which defines the types of your elements, then <element></element>can actually only be used for string types. Because, that's what it is, it is actually an empty string value: "".

首先,这取决于您的数据类型。如果您使用 XSD 架构定义,它定义了您的元素的类型,那么<element></element>实际上只能用于字符串类型。因为,就是这样,它实际上是一个空字符串值:""

Therefor, it is illegal to use <element></element>for an integer, while by contrast <element/>is applicable for all simple types.

因此,<element></element>对于整数使用是非法的,而相比之下<element/>适用于所有简单类型。

JAXB differences:

JAXB 差异:

JAXB (Java XML Binding) has similar quirks. It would map <element/>as different values depending on the target data type:

JAXB(Java XML 绑定)也有类似的怪癖。它会根据目标数据类型映射<element/>不同的值

  • for String, it would be a ""
  • for Integer, it would be a 0.
  • for other types, it would just skip it.
  • 对于字符串,它将是一个“”
  • 对于整数,它将是 0。
  • 对于其他类型,它只会跳过它。

You could be tempted to think that <element/>would resolve to a nullvalue. But you actually need to use <element xsi:nil="true"/>for that. Which I have never ever seen in practice.

您可能会认为这<element/>将解析为一个null值。但你实际上需要使用<element xsi:nil="true"/>它。这是我在实践中从未见过的。

HTML:

HTML:

In HTML there is always that third option of <element>without closing tag. I don't know about you, but several times a day, I have to remind myself that HTML isn't just XML. And that actually means that <br>and <br/>aren't the same thing. You shouldn't use <br/>in HTML, and you shouldn't use <br>in XHTML.

在 HTML 中,总是有第三个选项<element>没有关闭标签。我不了解你,但每天有好几次,我必须提醒自己 HTML 不仅仅是 XML。这实际上意味着这<br><br/>不是一回事。您不应该<br/>在 HTML 中使用,也不应该<br>在 XHTML 中使用。

Angular 2+:

角度 2+:

But what I really wanted to get to, continuing on the last statement, if you use frameworks like Angular2+ there is another thing to keep in mind.

但我真正想说的是,继续最后一个声明,如果您使用像 Angular2+ 这样的框架,还有另一件事要记住。

Without diving too much in detail, Angular replaces custom HTML tags with HTML templates which are linked to components. However it only supports the <element></element>format. You're not allowed to use the <element/>format.

Angular 没有过多深入地介绍,而是用链接到组件的 HTML 模板替换了自定义 HTML 标签。但是它只支持<element></element>格式。不允许使用该<element/>格式。

I personally try to avoid the <element/>syntax, because it has all the potential to make things go wrong. (= bad aji).

我个人尽量避免使用这种<element/>语法,因为它有可能让事情出错。(=坏阿吉)。

回答by y_a_v_a

When you want to use PHP's xml parser, you can specify actionsupon a start tag, close tag, or a complete tag. Based upon your wishes, you might want to have an action specifically on the close tag.

当您想使用 PHP 的xml parser 时,您可以指定对开始标记、结束标记或完整标记的操作。根据您的意愿,您可能希望对关闭标签进行专门的操作。

回答by Femi

In certain instances the separate open/close tags are necessary: as an example I've found that <script> tags in HTML need to have separate open/close to work consistently in some browsers. The separate open/close are a bit more verbose but worth it in those instances.

在某些情况下,单独的打开/关闭标签是必要的:例如,我发现 HTML 中的 <script> 标签需要有单独的打开/关闭标签才能在某些浏览器中一致地工作。单独的打开/关闭有点冗长,但在这些情况下值得。

回答by Lars

They are the same for XML, but may be different depending on your usage. For example, in xhtml1.0, there's an official listfor tags allowed for self-closing. As long as you do keep your doctype to the correct xhtml one, you should be fine. Check out this questionof stackoverflow for a more detailled explanation.

它们对于 XML 是相同的,但根据您的使用情况可能会有所不同。例如,在 xhtml1.0 中,有一个允许自动关闭的标签的官方列表。只要您确实将 doctype 保持为正确的 xhtml ,您应该没问题。查看stackoverflow 的这个问题以获得更详细的解释。