DOMElement::getAttributeNS()函数是PHP中的内置函数,用于获取具有当前节点本地名称的特定名称空间中的属性值。
用法:
string DOMElement::getAttributeNS( string $namespaceURI,
string $localName )
参数:该函数接受上述和以下描述的两个参数:
$namespaceURI:它指定名称空间URI。
$localName:它指定本地名称。
返回值:此函数返回包含属性值的字符串值,如果找不到具有给定localName和namespaceURI的属性,则返回空字符串。
以下示例说明了PHP中的DOMElement::getAttributeNS()函数:
范例1:
// Create a new DOMDocument
$dom = new DOMDocument();
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
");
// Get the elements by tagname
$elements = $dom->getElementsByTagName('div');
// Get the attribute node value
$nodeValue = $elements[0]->getAttributeNS('my_namespace', 'attr');
echo $nodeValue;
?>
输出:
value
范例2:
// Create a new DOMDocument
$dom = new DOMDocument();
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
");
// Get the elements by tagname
$elements = $dom->getElementsByTagName('div');
foreach ($elements as $element) {
// Get node value only from my_namespace1
$nodeValue = $element->getAttributeNS('my_namespace1', 'id');
if ($nodeValue) {
echo $nodeValue . '
';
}
}
?>
输出:
my_id1
my_id2