首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

SimpleXMLElement::getDocNamespaces

(PHP 5 >= 5.1.2, PHP 7)

SimpleXMLElement :: getDocNamespaces - 返回在文档中声明的名称空间

描述

代码语言:javascript
复制
public array SimpleXMLElement::getDocNamespaces ([ bool $recursive = false [, bool $from_root = true ]] )

返回文档中声明的名称空间

参数

recursive

如果指定,则返回在父节点和子节点中声明的所有名称空间。否则,只返回在根节点中声明的名称空间。

from_root

允许您递归检查子节点下的名称空间,而不是XML文档的根目录下的名称空间。

返回值

getDocNamespaces方法返回命名空间的名称与它们的相关联的URI的阵列。

例子

示例#1 获取文档名称空间

代码语言:javascript
复制
<?php

$xml?=?<<<XML
<?xml?version="1.0"?standalone="yes"?>
<people?xmlns:p="http://example.org/ns">
????<p:person?id="1">John?Doe</p:person>
????<p:person?id="2">Susie?Q.?Public</p:person>
</people>
XML;
?
$sxe?=?new?SimpleXMLElement($xml);

$namespaces?=?$sxe->getDocNamespaces();
var_dump($namespaces);

?>

上面的例子将输出:

代码语言:javascript
复制
array(1) {
   ["p"]=>
   string(21) "http://example.org/ns"
}

示例#2 使用多个名称空间

代码语言:javascript
复制
<?php

$xml?=?<<<XML
<?xml?version="1.0"?standalone="yes"?>
<people?xmlns:p="http://example.org/ns"?xmlns:t="http://example.org/test">
????<p:person?t:id="1">John?Doe</p:person>
????<p:person?t:id="2"?a:addr="123?Street"?xmlns:a="http://example.org/addr">
????????Susie?Q.?Public
????</p:person>
</people>
XML;
?
$sxe?=?new?SimpleXMLElement($xml);

$namespaces?=?$sxe->getDocNamespaces(TRUE);
var_dump($namespaces);

?>

上面的例子将输出:

代码语言:javascript
复制
array(3) {
  ["p"]=>
  string(21) "http://example.org/ns"
  ["t"]=>
  string(23) "http://example.org/test"
  ["a"]=>
  string(23) "http://example.org/addr"
}

更新日志

版本

描述

5.4.0

添加from_root参数。

扩展内容

  • SimpleXMLElement :: getNamespaces() - 返回文档中使用的名称空间
  • SimpleXMLElement :: registerXPathNamespace() - 为下一个XPath查询创建前缀/ ns上下文

← SimpleXMLElement::count

SimpleXMLElement::getName →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com