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 — Comprueba si un atributo existe
Descripción
bool DOMElement::hasAttributeNS
( string
$namespaceURI
, string $localName
)
Indica si un atributo en el espacio de nombres namespaceURI
llamado localName existe como miembro del elemento.
Parámetros
-
namespaceURI -
La URI del espacio de nombres.
-
localName -
El nombre local.
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
Ver también
- DOMElement::hasAttribute() - Comprueba si existe un atributo
- DOMElement::getAttributeNS() - Devuelve el valor de un atributo
- DOMElement::setAttributeNS() - Añade un nuevo atributo
- DOMElement::removeAttributeNS() - Elimina un atributo
chad dot retz at gmail dot com ¶
5 years ago
