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

SimpleXMLElement::getNamespaces

(PHP 5 >= 5.1.2, PHP 7)

SimpleXMLElement :: getNamespaces - 返回文档中使用的命名空间

描述

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

返回文档中使用的名称空间

参数

recursive

如果指定,则返回父节点和子节点中使用的所有名称空间。否则,仅返回根节点中使用的名称空间。

返回值

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

例子

示例#1 获取正在使用的文档名称空间

代码语言: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 id="1">John Doe</p:person>
    <p:person id="2">Susie Q. Public</p:person>
</people>
XML;
 
$sxe = new SimpleXMLElement($xml);

$namespaces = $sxe->getNamespaces(true);
var_dump($namespaces);

?>

上面的例子将输出:

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

扩展内容

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

← SimpleXMLElement::getName

SimpleXMLElement::registerXPathNamespace →

扫码关注腾讯云开发者

领取腾讯云代金券