(PHP 5 >= 5.1.2, PHP 7, PHP 8)
SimpleXMLElement::getDocNamespaces — 傳回文件中宣告的命名空間
返回文件中宣告的命名空間
recursive (遞迴)
如果指定,則返回在父節點和子節點中宣告的所有命名空間。否則,僅返回在根節點中宣告的命名空間。
fromRoot (從根節點開始)
允許您遞迴檢查子節點下的命名空間,而不是從 XML 文件的根節點開始。
getDocNamespaces
方法返回一個 陣列,其中包含命名空間名稱及其關聯的 URI。
範例 #1 取得文件命名空間
<?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);
?>
以上範例將輸出
array(1) { ["p"]=> string(21) "http://example.org/ns" }
範例 #2 使用多個命名空間
<?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);
?>
以上範例將輸出
array(3) { ["p"]=> string(21) "http://example.org/ns" ["t"]=> string(23) "http://example.org/test" ["a"]=> string(23) "http://example.org/addr" }