SimpleXML SimpleXMLElement::getDocNamespaces
(PHP 5 >= 5.1.2, PHP 7, PHP 8)
SimpleXMLElement::getDocNamespaces — 문서에 선언된 네임스페이스를 반환합니다.
설명
public SimpleXMLElement::getDocNamespaces(bool $recursive
= false
, bool $fromRoot
= true
): array|false
문서에 선언된 네임스페이스를 반환합니다.
매개변수
recursive
- 지정하면 부모 및 자식 노드에 선언된 모든 네임스페이스를 반환합니다. 그렇지 않으면 루트 노드에 선언된 네임스페이스만 반환합니다.
fromRoot
- XML 문서의 루트 대신 자식 노드 아래의 네임스페이스를 재귀적으로 확인할 수 있습니다.
반환 값
getDocNamespaces 메소드는 연관된 URI와 함께 네임스페이스 이름의 배열을 리턴합니다.
Examples
예제 #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" }
기타
- SimpleXMLElement::getNamespaces() - 문서에 사용된 네임스페이스를 반환합니다.
- SimpleXMLElement::registerXPathNamespace() - 다음 XPath 쿼리에 대한 접두사/ns 컨텍스트를 만듭니다.