问题
Two method of SimpleXMLElement
are getDocNamespaces
and getNamespaces
. Both seems same to me, i tried the example of each other changing the method name but same result.
So can anyone explain it to me, what is the difference? Links from php.net getNamespaces getdocnamespaces
回答1:
getDocNamespaces() returns all namespaces declared in document; getNamespaces() returns only those namespaces that are actually used in the document.
Example #1 in the getNamespaces() doc page shows this with xmlns:t="http://example.org/test"
defined, but not used.
回答2:
Actually those methods do two similar but different things. Both retrieve namespaces. Both can even do that recursively. Both can operate on elements.
The difference is to obtain namespaces used (###:element
) with/on specific elements or defined (xmlns:###="XMLNS-URI"
) on specific elements.
Some examples:
getNamespaces
obtains used namespaces. E.g. if you've got a person element somewhere in the tree like:
...
<p:person t:id="1">John Doe</p:person>
...
And you use
$sxe->person[0]->getNamespaces(TRUE);
on it, it will give you two namespaces, those two used on that element:
[p] => http://example.org/ns
[t] => http://example.org/test
getDocNamespaces
in difference, when operating on that same element will give you the namespaces defined:
$sxe->person[0]->getDocNamespaces(TRUE, FALSE);
As that element does not define any, the array is empty.
In difference to that, on a different, second person element:
...
<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);
will give you exactly one namespace because it's defined there:
[a] => http://example.org/addr
This should clarfiy it. You can permutate a bit through the diverse parameter combos, so here is some example script (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";
Output:
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
)
See as well:
- What are the “$ns” and “$is_prefix” parameters about?
来源:https://stackoverflow.com/questions/18351002/difference-between-getdocnamespaces-and-getnamespaces-in-simplexml