との 2 つの方法SimpleXMLElement
がgetDocNamespaces
ありgetNamespaces
ます。どちらも同じように思えますが、メソッド名を変更してお互いの例を試してみましたが、結果は同じでした。
誰でも説明できますか、違いは何ですか?php.net getNamespaces getdocnamespacesからのリンク
との 2 つの方法SimpleXMLElement
がgetDocNamespaces
ありgetNamespaces
ます。どちらも同じように思えますが、メソッド名を変更してお互いの例を試してみましたが、結果は同じでした。
誰でも説明できますか、違いは何ですか?php.net getNamespaces getdocnamespacesからのリンク
実際、これらのメソッドは 2 つの似ているが異なることを行います。どちらも名前空間を取得します。どちらもそれを再帰的に行うことさえできます。どちらも要素を操作できます。
違いは、特定の要素で使用される( )###:element
または特定の要素で定義される( )名前空間を取得することxmlns:###="XMLNS-URI"
です。
いくつかの例:
getNamespaces
使用済みの名前空間を取得します。たとえば、次のようなツリーのどこかに person 要素があるとします。
...
<p:person t:id="1">John Doe</p:person>
...
そして、あなたは使用します
$sxe->person[0]->getNamespaces(TRUE);
その上で、その要素で使用される2つの名前空間が提供されます。
[p] => http://example.org/ns
[t] => http://example.org/test
getDocNamespaces
違いは、同じ要素を操作すると、定義された名前空間が得られることです。
$sxe->person[0]->getDocNamespaces(TRUE, FALSE);
その要素は何も定義していないため、配列は空です。
それとは異なり、別の二人称要素では:
...
<p:person t:id="2" a:addr="123 Street" xmlns:a="http://example.org/addr">
Susie Q. Public
</p:person>
...
$sxe->person[1]->getDocNamespaces(TRUE, FALSE);
そこで定義されているため、正確に1つの名前空間が提供されます。
[a] => http://example.org/addr
これはそれを明確にするはずです。さまざまなパラメーターの組み合わせを少し変更できるため、スクリプトの例 ( Demo )を次に示します。
<?php
/**
* @link https://stackoverflow.com/a/18354621/367456
*/
$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, 0, FALSE, 'http://example.org/ns');
echo 'person[0]->getNamespaces(TRUE): ',
!print_r($sxe->person[0]->getNamespaces(TRUE)), "\n";
echo 'person[0]->getNamespaces(FALSE): ',
!print_r($sxe->person[0]->getNamespaces(FALSE)), "\n";
echo 'person[0]->getDocNamespaces(TRUE): ',
!print_r($sxe->person[0]->getDocNamespaces(TRUE)), "\n";
echo 'person[0]->getDocNamespaces(TRUE, FALSE): ',
!print_r($sxe->person[0]->getDocNamespaces(TRUE, FALSE)), "\n";
echo 'person[1]->getDocNamespaces(TRUE, FALSE): ',
!print_r($sxe->person[1]->getDocNamespaces(TRUE, FALSE)), "\n";
echo 'sxe->getNamespaces(FALSE): ',
!print_r($sxe->getNamespaces(FALSE)), "\n";
echo 'sxe->getDocNamespaces(TRUE, FALSE): ',
!print_r($sxe->getDocNamespaces(TRUE, FALSE)), "\n";
echo 'sxe->getDocNamespaces(FALSE, FALSE): ',
!print_r($sxe->getDocNamespaces(FALSE, FALSE)), "\n";
echo 'sxe->getNamespaces(TRUE): ',
!print_r($sxe->getNamespaces(TRUE)), "\n";
echo 'sxe->getDocNamespaces(TRUE): ',
!print_r($sxe->getDocNamespaces(TRUE)), "\n";
出力:
person[0]->getNamespaces(TRUE): Array
(
[p] => http://example.org/ns
[t] => http://example.org/test
)
person[0]->getNamespaces(FALSE): Array
(
[p] => http://example.org/ns
[t] => http://example.org/test
)
person[0]->getDocNamespaces(TRUE): Array
(
[p] => http://example.org/ns
[t] => http://example.org/test
[a] => http://example.org/addr
)
person[0]->getDocNamespaces(TRUE, FALSE): Array
(
)
person[1]->getDocNamespaces(TRUE, FALSE): Array
(
[a] => http://example.org/addr
)
sxe->getNamespaces(FALSE): Array
(
)
sxe->getDocNamespaces(TRUE, FALSE): Array
(
[p] => http://example.org/ns
[t] => http://example.org/test
[a] => http://example.org/addr
)
sxe->getDocNamespaces(FALSE, FALSE): Array
(
[p] => http://example.org/ns
[t] => http://example.org/test
)
sxe->getNamespaces(TRUE): Array
(
[p] => http://example.org/ns
[t] => http://example.org/test
[a] => http://example.org/addr
)
sxe->getDocNamespaces(TRUE): Array
(
[p] => http://example.org/ns
[t] => http://example.org/test
[a] => http://example.org/addr
)
同様に参照してください: