查询

DOMAttr::isId()函数—用法及示例

「 检查 DOM 属性是否被定义为 ID 属性 」


PHP版本:5.1.2及以上

用法: DOMAttr::isId() 方法用于检查 DOM 属性是否被定义为 ID 属性。

语法: bool DOMAttr::isId ( void )

参数: 此方法不接受任何参数。

返回值: 如果属性被定义为 ID 属性,则返回 true,否则返回 false。

示例:

$xmlString = '<bookstore>
  <book id="1">Harry Potter</book>
  <book>Lord of the Rings</book>
</bookstore>';

$dom = new DOMDocument();
$dom->loadXML($xmlString);

$books = $dom->getElementsByTagName('book');

foreach ($books as $book) {
    if ($book->hasAttributes()) {
        foreach ($book->attributes as $attr) {
            if ($attr->nodeName === 'id') {
                if ($attr->isId()) {
                    echo $attr->nodeValue . " is defined as an ID attribute.";
                } else {
                    echo $attr->nodeValue . " is not defined as an ID attribute.";
                }
            }
        }
    }
}

此示例会输出:

1 is defined as an ID attribute.
补充纠错
上一个函数: DocResult::__construct()函数
下一个函数: DOMAttr::__construct()函数
热门PHP函数
分享链接