查詢

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函式
分享連結