DOMElement::getAttributeNS()函数是PHP中的内置函数,用于获取具有当前节点本地名称的特定名称空间中的属性值。
用法:
string DOMElement::getAttributeNS( string $namespaceURI, string $localName )
参数:该函数接受上述和以下描述的两个参数:
- $namespaceURI:它指定名称空间URI。
 - $localName:它指定本地名称。
 
返回值:此函数返回包含属性值的字符串值,如果找不到具有给定localName和namespaceURI的属性,则返回空字符串。
以下示例说明了PHP中的DOMElement::getAttributeNS()函数:
范例1:
<?php 
  
// Create a new DOMDocument 
$dom = new DOMDocument(); 
  
// Load the XML 
$dom->loadXML("<?xml version=\"1.0\"?> 
<body xmlns:x=\"my_namespace\"> 
    <x:div x:attr=\"value\" > DIV 1 </x:div> 
</body>"); 
  
// 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:
<?php 
  
// Create a new DOMDocument 
$dom = new DOMDocument(); 
  
// Load the XML 
$dom->loadXML("<?xml version=\"1.0\"?> 
<root> 
<body xmlns:x=\"my_namespace1\"> 
    <x:div x:id=\"my_id1\" > DIV 1 </x:div> 
    <x:div x:id=\"my_id2\" > DIV 1 </x:div> 
</body> 
<body xmlns:xi=\"my_namespace2\"> 
    <xi:div xi:id=\"new\" > DIV 1 </xi:div> 
</body> 
</root>"); 
  
// 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 . '<br>'; 
    } 
} 
?>输出:
my_id1 my_id2
参考: https://www.php.net/manual/en/domelement.getattributens.php
相关用法
- PHP DOMElement getElementsByTagName()用法及代码示例
 - PHP DOMElement hasAttributeNS()用法及代码示例
 - PHP DOMElement setAttributeNS()用法及代码示例
 - PHP DOMElement setAttributeNodeNS()用法及代码示例
 - PHP DOMElement getAttributeNodeNS()用法及代码示例
 - PHP DOMElement getAttribute()用法及代码示例
 - PHP DOMElement __construct()用法及代码示例
 - PHP DOMElement getAttributeNode()用法及代码示例
 - PHP DOMElement removeAttribute()用法及代码示例
 - PHP DOMElement removeAttributeNS()用法及代码示例
 - PHP DOMElement setIdAttribute()用法及代码示例
 - PHP DOMElement setIdAttributeNode()用法及代码示例
 - PHP DOMElement setIdAttributeNS()用法及代码示例
 
注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | DOMElement getAttributeNS() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
