Skip to content

Core\Xml\Node::namespaceUri

$node->namespaceUri(): ?tainted string

Returns the namespace URI of an XML element, or null if the element is in no namespace.

A namespace is a URI that says which vocabulary a name belongs to. A document declares one with xmlns:x="…" and then writes names such as <x:title>. The declaration covers the element that writes it and every element inside it. A name without a prefix uses the nearest xmlns="…". An inner declaration replaces an outer one.

The result is null for a node that is not an element, and for an element that no declaration covers. The name does not change: name still returns x:title. The result is tainted, because it came from outside the program.

The examples below print the namespace of each element, show an inner declaration that replaces an outer one, and find elements by their namespace when two documents use different prefixes.

Return value

Returns ?tainted string. A tainted string, or null. The result is null for a node that is not an element, for an element that no declaration covers, and for an element under xmlns="". The name does not change: name still returns x:item.

Examples

The namespace of each element

<?nvs
// Shows the namespace of each element in a small document.
string $xml = '<shop xmlns="https://example.com/shop" xmlns:p="https://example.com/price">'
. '<item><p:amount>4.50</p:amount></item></shop>';
// `shop` and `item` have no prefix, so they use the `xmlns` URI.
// `p:amount` uses the URI of the prefix `p`. This prints each name and its namespace.
foreach (Core\Xml::parse($xml)->children() as Core\Xml\Node $shop) {
echo $shop->name(), ' ', $shop->namespaceUri() ?? 'none', "\n";
foreach ($shop->children() as Core\Xml\Node $item) {
echo $item->name(), ' ', $item->namespaceUri() ?? 'none', "\n";
foreach ($item->children() as Core\Xml\Node $amount) {
echo $amount->name(), ' ', $amount->namespaceUri() ?? 'none', "\n";
}
}
}
// A document without declarations has no namespace, so this prints none.
foreach (Core\Xml::parse('<note/>')->children() as Core\Xml\Node $note) {
echo $note->namespaceUri() ?? 'none', "\n";
}
Output
shop https://example.com/shop
item https://example.com/shop
p:amount https://example.com/price
none

An inner declaration

<?nvs
// Shows that a declaration inside the document replaces the one outside it.
string $xml = '<page xmlns="https://example.com/page">'
. '<svg xmlns="https://example.com/svg"><circle/></svg>'
. '<plain xmlns=""><line/></plain>'
. '</page>';
// `svg` declares its own namespace, so `svg` and `circle` are in it.
// `xmlns=""` removes the namespace, so `plain` and `line` return `null`.
foreach (Core\Xml::parse($xml)->children() as Core\Xml\Node $page) {
foreach ($page->children() as Core\Xml\Node $part) {
echo $part->name(), ' ', $part->namespaceUri() ?? 'none', "\n";
foreach ($part->children() as Core\Xml\Node $inner) {
echo $inner->name(), ' ', $inner->namespaceUri() ?? 'none', "\n";
}
}
}
// A text node is not an element, so its namespace is `null`. This prints none.
foreach (Core\Xml::parse('<a xmlns="https://example.com/a">hi</a>')->children() as Core\Xml\Node $a) {
foreach ($a->children() as Core\Xml\Node $text) {
echo $text->namespaceUri() ?? 'none', "\n";
}
}
Output
svg https://example.com/svg
circle https://example.com/svg
plain none
line none
none

Find authors in any feed

<?nvs
// Shows how to find the authors in two feeds that use different prefixes for the same namespace.
// Each feed chooses its own prefix. Only the URI tells you what the name means.
string $dublinCore = 'http://purl.org/dc/elements/1.1/';
array<string> $feeds = [
'<rss xmlns:dc="http://purl.org/dc/elements/1.1/"><item><dc:creator>Ana</dc:creator></item></rss>',
'<rss xmlns:meta="http://purl.org/dc/elements/1.1/"><item><meta:creator>Ben</meta:creator></item>'
. '<item><creator>Not an author</creator></item></rss>',
];
class Authors
{
// This function prints the text of every `creator` element in the namespace `$uri`.
public static function print(Core\Xml\Node $node, string $uri): void
{
// `Core\Str::after` removes the prefix. A name without a prefix stays as it is.
tainted string $local = Core\Str::after($node->name(), ':') ?? $node->name();
if ($local == 'creator' && $node->namespaceUri() == $uri) {
foreach ($node->children() as Core\Xml\Node $text) {
echo $text->text(), "\n";
}
}
foreach ($node->children() as Core\Xml\Node $child) {
Authors::print($child, $uri);
}
}
}
// The second `creator` in the second feed has no namespace, so it is skipped.
// This prints Ana and then Ben.
foreach ($feeds as string $feed) {
Authors::print(Core\Xml::parse($feed), $dublinCore);
}
Output
Ana
Ben