PHP getDocNamespaces() 函数

定义和用法

getDocNamespaces() 函数返回 XML 文档中声明的命名空间。

实例

例子 1

返回 XML 文档根节点中声明的命名空间:

<?php
$xml=<<<XML
<?xml version="1.0" standalone="yes"?>
<cars xmlns:c="http://w3school.com.cn/ns">
  <c:car id="1">Volvo</c:car>
  <c:car id="2">BMW</c:car>
  <c:car id="3">Saab</c:car>
</cars>
XML;

$sxe=new SimpleXMLElement($xml);
$ns=$sxe->getDocNamespaces();
print_r($ns);
?>

运行实例

例子 2

返回 XML 文档中声明的所有命名空间:

<?php
$xml=<<<XML
<?xml version="1.0" standalone="yes"?>
<cars xmlns:c="http://w3school.com.cn/ns">
  <c:car id="1">Volvo</c:car>
  <c:car id="2">BMW</c:car>
  <c:car id="3" a:country="Sweden" xmlns:a="http://w3school.com.cn/country">Saab</c:car>
</cars>
XML;

$sxe=new SimpleXMLElement($xml);
$ns=$sxe->getDocNamespaces(TRUE);
var_dump($ns);
?>

运行实例

语法

SimpleXMLElement::getDocNamespaces(recursive, from_root)
参数 描述
recursive

可选。指定布尔值。

如果为 TRUE,则返回文档中声明的所有命名空间;

如果为 FALSE,则仅返回根节点中声明的命名空间。

默认为 FALSE。

from_root

可选。指定布尔值。

TRUE 从 XML 文档的根节点检查命名空间;

FALSE 从子节点检查命名空间。

默认为 TRUE。

技术细节

返回值: 返回包含命名空间名称及其关联 URI 的数组。
PHP 版本: 5.1.2+
PHP Changelog: PHP 5.4:添加了 from_root 参数。