XML 文件中的自闭合标签

2023-07-31前端开发问题
100

本文介绍了XML 文件中的自闭合标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

<tag id="foo"/>

我注意到它们可以使用 PHP SimpleXML.

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

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

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

解决方案

查看 XML 和 XHTML 的 W3C 规范:

  • 中定义为

    <块引用>

    这个词或形容词推荐"的意思是在特定情况下可能存在忽略特定项目,但必须了解全部含义并在选择不同的课程之前仔细权衡.

    如果您正在使用没有 DTD 或模式的 XML,您还可以使用 预定义的 libxml 常量:

    <块引用>

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

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

    <tag id="foo" />

    I noticed that they work with PHP SimpleXML.

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

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

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

    解决方案

    See the W3C specs for XML and XHTML:

    • http://www.w3.org/TR/xml/#sec-starttags
    • http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_XHTML-1.0-Strict

    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.

    This means, when your DTD contains something like

    <!ELEMENT img EMPTY>
    

    you should use

    <img/>
    

    unless you have good reason to use

    <img></img>
    

    Note that SHOULD is defined in RFC2119 as

    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:

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

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

    这篇关于XML 文件中的自闭合标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13

getFullYear 在一年的第一天返回前一年
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20 前端开发问题
6