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

问题描述

< tag id =foo/>



我注意到它们使用PHP SimpleXML 。

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

< tag id =foo>< / tag>



是否有任何理由使用旧方法?

解决方案

请参阅XML和XHTML的W3C规范:





  • 它取决于元素类型声明

    一个空元素的表示形式是一个紧跟着一个结束标签的开始标签或一个空元素标签。而且还包含

    这意味着,当你的DTD包含像

     <!ELEMENT img EMPTY> 

    使用

     < img /> 

    除非您有充足的理由使用

     < IMG>< / IMG> 

    请注意:

    但是请注意,此选项目前仅在函数中可用 DOMDocument :: save DOMDocument :: 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:

    It depends on the Element Type declaration

    but also

    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

    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:

    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文件中的自闭标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-05 12:58
    查看更多