函数名:DOMElement::hasAttributeNS()
适用版本:5.2及以上版本
用法:该函数用于检查DOM元素是否具有指定命名空间中具有给定本地名称的属性。
语法:bool DOMElement::hasAttributeNS ( string $namespaceURI , string $qualifiedName )
参数:
- $namespaceURI:属性的命名空间URI。
- $qualifiedName:属性的限定名称。
返回值:
- 如果DOM元素具有指定命名空间中给定本地名称的属性,则返回true。
- 如果DOM元素不具有指定命名空间中给定本地名称的属性,则返回false。
示例:
$xmlString = '<root xmlns:example="http://www.example.com" example:attribute="value"></root>';
$domDocument = new DOMDocument();
$domDocument->loadXML($xmlString);
$rootElement = $domDocument->documentElement;
$hasAttribute = $rootElement->hasAttributeNS('http://www.example.com', 'attribute');
if ($hasAttribute) {
echo "DOM元素具有指定命名空间中给定本地名称的属性。";
} else {
echo "DOM元素不具有指定命名空间中给定本地名称的属性。";
}
上面的示例中,我们创建了一个XML字符串并将其加载到DOM文档中。然后,我们获取根元素,并使用hasAttributeNS()
函数检查该元素是否具有命名空间URI为'http://www.example.com',本地名称为'attribute'的属性。如果属性存在,则输出"DOM元素具有指定命名空间中给定本地名称的属性。",否则输出"DOM元素不具有指定命名空间中给定本地名称的属性。"