DOM Error - ID 'someAnchor' already defined in Entity, line X

前端 未结 1 1527
一生所求
一生所求 2020-12-07 02:09

If I try to load an HTML document into PHP DOM I get an error along the lines of:

Error DOMDocument::loadHTML() [domdocument.loadhtml]: ID someAnchor already         


        
相关标签:
1条回答
  • 2020-12-07 02:27

    If you are loading XML files (that's the case, XHTML is XML), then you should use DOMDocument::loadXML(), not DOMDocument::loadHTML().

    In HTML, both name and id introduce an ID. So you are repeating the id "someAnchor", hence the error.

    However, the W3C validator allows repeated IDs in the form you show <a id="someAnchor" name="someAnchor"></a>. This may be a bug of libmxl2.

    In this bug report for libxml2, a user proposes a patch to only consider the name attribute as an ID:

    According to the HTML and XHTML specs, only the a element's name attribute shares name space with id attributes. For some of the elements it can be argued that multiple instances with the same name don't make sense, but they should nevertheless not be considered in the same namespace as other elements' id attributes.

    See http://www.zvon.org/xxl/xhtmlReference/Output/Strict/attr_name.html for all the elements that take name attributes and their semantics.

    0 讨论(0)
提交回复
热议问题