Core\Xml\Node::name
Returns the name of an XML node, as a string.
For an element such as <price>, the name is price. For a processing instruction such as <?print fast?>, the name is print. A text node, a comment and the document have no name, so the result is an empty string.
The name is exactly as the document writes it. If the tag has a prefix, the prefix is part of the name: for <media:title> the result is media:title. Use namespaceUri to find out what the prefix means. The result is tainted, because it came from outside the program.
The examples below print the name of every node, show a name with a prefix, and add up the prices in an order by looking for elements by name.
Return value
Returns tainted string. A tainted string. The name is written as in the document, with its prefix: for <x:a/> the result is x:a. To find the namespace of the prefix, use namespaceUri.
Examples
The name of every node
<?nvs// Shows the name of each kind of node.
Core\Xml\Node $document = Core\Xml::parse('<?print fast?><order><!--gift--><item>Mug</item></order>');
// The processing instruction is named `print` and the element is named `order`.// This prints [print] and then [order].foreach ($document->children() as Core\Xml\Node $node) { echo '[', $node->name(), "]\n";}
// Inside `<order>`, the comment has no name, so this prints [] and then [item].foreach ($document->children() as Core\Xml\Node $node) { foreach ($node->children() as Core\Xml\Node $child) { echo '[', $child->name(), "]\n"; }}[print][order][][item]A name with a prefix
<?nvs// Shows that a prefix is part of the name.
string $xml = '<feed xmlns:media="https://example.com/media"><media:title>Cats</media:title></feed>';
foreach (Core\Xml::parse($xml)->children() as Core\Xml\Node $feed) { foreach ($feed->children() as Core\Xml\Node $title) { // This prints media:title. echo $title->name(), "\n"; // The name is not `title`, so this prints no. echo $title->name() == 'title' ? 'yes' : 'no', "\n"; // `namespaceUri` gives the namespace of the prefix: https://example.com/media echo $title->namespaceUri() ?? '', "\n"; }}media:titlenohttps://example.com/mediaAdd up the prices in an order
<?nvs// Shows how to find every element with one name, at any depth, and add up their values.
string $xml = '<order> <line><product>Mug</product><price>4.50</price></line> <line><product>Teapot</product><price>19.90</price></line> <shipping><price>5.00</price></shipping></order>';
// The loop reads the next node in the list and adds its children to the end of the list.// In this way it visits every node in the document.array<Core\Xml\Node> $todo = [Core\Xml::parse($xml)];int $next = 0;float $total = 0.0;while ($next < Core\Arr::count($todo)) { Core\Xml\Node $node = $todo[$next]; $next = $next + 1; if ($node->name() == 'price') { foreach ($node->children() as Core\Xml\Node $text) { $total = $total + ($text->text() as ?float ?? 0.0); } } foreach ($node->children() as Core\Xml\Node $child) { $todo[] = $child; }}
// This prints 29.4.echo $total, "\n";29.4