【问题标题】:Why are warning messages generated about duplicate IDs when there are none?为什么在没有重复 ID 时会生成有关重复 ID 的警告消息?
【发布时间】:2010-07-28 00:05:13
【问题描述】:

以下是服务器给我的意外警告消息:

警告:DOMDocument::loadHTML() [domdocument.loadhtml]: ID page3 已在实体中定义,行:C:\Program Files\Zend\Apache2\htdocs\joom\templates\valueTemplate\updateRecord 中的第 25 行.php 在第 74 行 .

.

上面的警告信息是在执行以下代码时产生的:

$html = new DOMDocument();
$html->loadHTML( $fetchedData[ 'content' ] );

.

该消息出乎意料,因为在 HTML 文档中没有重复使用“page3”作为 ID。然而,'page3' 在 HTML 文档中被多次用作 name 属性的值。例如:

<li id="index00025" name="page3" class="fooBar"></li>

.

对此的任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 我不知道这是否会导致 DOMDocument 出现问题,但我很确定 name 不是 &lt;li&gt; 的有效属性
  • 我的其他 PHP 页面包含的元素具有无效的 'name' 属性,但是当使用 DOMDocument 加载 HTML 内容时,不会生成任何警告。
  • 您的文档中是否有任何命名的锚点,例如&lt;a name="page3"&gt;? HTML 规范在锚点上将 name 和 id 属性视为相同:w3.org/TR/html401/struct/links.html#h-12.2.1。可能在 &lt;li&gt; 上具有无效的名称属性会混淆 loadHTML 方法。
  • 谢谢,帕特。我不知道。

标签: php domdocument


【解决方案1】:

这是预期的行为。在HTML中,属性“name”引入了一个id,就像属性“id”本身一样,以防元素为a(我对libxml内部一无所知,所以不知道在什么情况下@ 987654322@可以为NULL)。

/**
 * xmlIsID:
 * [...]
 *
 * Determine whether an attribute is of type ID. In case we have DTD(s)
 * then this is done if DTD loading has been requested. In the case
 * of HTML documents parsed with the HTML parser, then ID detection is
 * done systematically.
 * [...]
 */
int
xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
    [...]
    } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
        if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
        ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
        ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
        return(1);
    return(0);    
    }
    [...]
}

来源:libxml 发行版上的valid.c

【讨论】:

    【解决方案2】:

    Artefacto 是正确的。另一种看待它的方式——正如 HTML 规范所做的那样——是 nameid 属性共享相同的命名空间。因此,在其中一个属性中定义的标识符会显示在另一个属性的值集合中,即,如果您定义 name="foo",当您列出所有 name 属性或所有 id 时,foo 将显示属性。

    来源:http://www.w3.org/TR/html401/struct/links.html#h-12.2.3

    【讨论】:

      猜你喜欢
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 2023-03-09
      • 2018-12-09
      • 2015-08-18
      • 1970-01-01
      • 2011-10-23
      相关资源
      最近更新 更多