English | 简体中文 | 繁體中文
查询

DOMElement::getAttributeNS()函数—用法及示例

「 从当前元素节点中获取指定命名空间URI和本地名(local name)的属性值 」


函数名称:DOMElement::getAttributeNS() 

适用版本:PHP版本5及以上

函数用法:getAttributeNS() 方法用于从当前元素节点中获取指定命名空间URI和本地名(local name)的属性值。

语法:public DOMAttr|null DOMElement::getAttributeNS(string $namespaceURI, string $localName)

参数:

  • $namespaceURI:要获取的属性的命名空间URI。
  • $localName:要获取的属性的本地名。

返回值:

  • 如果找到匹配的属性,则返回 DOMAttr 对象,该对象表示要获取的属性。
  • 如果未找到匹配的属性,则返回 null。

示例:

// 创建一个新的 DOMDocument 对象
$document = new DOMDocument();

// 从 XML 字符串加载一个 DOM 树
$document->loadXML('<root xmlns:example="http://www.example.com"><example:node attribute="value"/></root>');

// 获取根元素
$root = $document->documentElement;

// 获取命名空间为 "http://www.example.com",本地名为 "attribute" 的属性值
$attributeValue = $root->getAttributeNS("http://www.example.com", "attribute");

// 打印属性值
echo $attributeValue;  // 输出: value

上述示例中,我们创建了一个包含一个命名空间为 "http://www.example.com" 的元素节点的 DOM 树。使用getAttributeNS() 方法,我们可以从根元素中获取名为 "attribute" 的属性的值,并将其打印出来。

补充纠错
热门PHP函数
分享链接