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'));
?>
DOMElement::hasAttributeNS
(PHP 5)
DOMElement::hasAttributeNS — 属性が存在するかどうかを調べる
説明
bool DOMElement::hasAttributeNS
( string $namespaceURI
, string $localName
)
要素のメンバとして localName という名前の属性が 名前空間 namespaceURI に存在するかどうかを示します。
パラメータ
- namespaceURI
-
名前空間 URI。
- localName
-
ローカル名。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
参考
- DOMElement::hasAttribute - 属性が存在するかどうかを調べる
- DOMElement::getAttributeNS - 属性の値を返す
- DOMElement::setAttributeNS - 新しい属性を追加する
- DOMElement::removeAttributeNS - 属性を削除する
DOMElement::hasAttributeNS
chad dot retz at gmail dot com
11-May-2008 04:38
11-May-2008 04:38
