php hasattribute,PHP DOMElement hasAttributeNS()用法及代码示例

DOMElement::hasAttributeNS()函数是PHP中的一个内置函数,用于了解名为localName的特定命名空间中的属性是否作为元素的成员存在。

用法:

bool DOMElement::hasAttributeNS( string $namespaceURI, string $localName )

参数:该函数接受上述和以下描述的两个参数:

$namespaceURI:它指定名称空间URI。

$localName:它指定本地名称。

返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。

以下示例说明了PHP中的DOMElement::hasAttributeNS()函数:

范例1:

// Create a new DOMDocument

$dom = new DOMDocument();

// Load the XML

$dom->loadXML("<?xml version=\"1.0\"?>

Hello, this is my red heading.

Hello, this is my green heading.

Hello, this is my blue heading.

Hello, this is my new red heading.

Hello, this is my new green heading.

Hello, this is my new blue heading.

");

// Get the elements

$nodeList = $dom->getElementsByTagName('h1');

$i = 0;

foreach ($nodeList as $node) {

if($node->hasAttributeNS('my_namespace', 'style')) {

$i++;

}

}

echo "Yes, there are total of $i style

attributes inside my_namespace";

?>

输出:

Yes, there are total of 3 style attributes inside my_namespace.

范例2:

// Create a new DOMDocument

$dom = new DOMDocument();

// Load the XML

$dom->loadXML("<?xml version=\"1.0\"?>

1

2

3

1

2

3

");

// Get the elements

$nodeList = $dom->getElementsByTagName('h1');

$i = 0;

foreach ($nodeList as $node) {

if($node->hasAttributeNS('another_namespace', 'id')) {

$i++;

}

}

echo "There are total of $i id attributes

inside another_namespace.";

?>

输出:

There are total of 0 id attributes inside another_namespace.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值