DOMElement
在线手册:中文 英文
PHP手册

DOMElement::hasAttributeNS

(PHP 5)

DOMElement::hasAttributeNS Checks to see if attribute exists

说明

bool DOMElement::hasAttributeNS ( string $namespaceURI , string $localName )

Indicates whether attribute in namespace namespaceURI named localName exists as a member of the element.

参数

namespaceURI

The namespace URI.

localName

The local name.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE.

参见


DOMElement
在线手册:中文 英文
PHP手册
PHP手册 - N: Checks to see if attribute exists

用户评论:

chad dot retz at gmail dot com (11-May-2008 08:38)

This does not work as expected (at least on 5.2.5) with attributes in the default namespace. For instance:

<?php
$dom
= new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><element xmlns="testns" attr="testval" />');
var_dump($dom->documentElement->hasAttributeNS('testns', 'attr'));
?>

returns bool(false) whereas:

<?php
$dom
= new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><element xmlns:ns1="testns" ns1:attr="testval" />');
var_dump($dom->documentElement->hasAttributeNS('testns', 'attr'));
?>

returns bool(true). NULL does work properly in the namespaceURI parameter, so changing my initial example to:

<?php
$dom
= new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><element xmlns="testns" attr="testval" />');
var_dump($dom->documentElement->hasAttributeNS(NULL, 'attr'));
?>

returns bool(true) as expected. Or even better for when you don't know whether the NS will be default:

<?php
$dom
= new DOMDocument();
$dom->loadXML('<?xml version="1.0"?><element xmlns="testns" attr="testval" />');
var_dump($dom->documentElement->hasAttributeNS(
   
is_null($dom->documentElement->lookupPrefix('testns')) ? NULL : 'testns', 'attr'));
?>