Skip to content

Core\Xml\Reader::depth

$reader->depth(): uint

Returns how deeply the node that read returned last is nested, as a whole number.

The root element has depth 0. A node inside the root element has depth 1, and each level adds one. A comment or a processing instruction outside the root element also has depth 0. Before the first read, and after read returns null, the depth is 0.

A reader does not return closing tags, so the depth is how you see where an element ends. When the next node has the same depth as an element, or a smaller one, that element is closed.

The examples below print a document as an outline, show the depth before and after the walk, and count the lines inside each order.

Return value

Returns uint. A whole number. It is 0 before the first read, and 0 again after read returns null.

Examples

Print an outline

<?nvs
// Shows how to print the elements of a document as an outline, indented by their depth.
Core\Xml\Reader $reader = Core\Xml::reader('<menu><file><open/><save/></file><help><about/></help></menu>');
// `depth` is 0 for the root element and one more for each level inside it.
// This prints menu, then file, open, save, help and about, each indented by two spaces per level.
?Core\Xml\Node $node = $reader->read();
while ($node != null) {
echo Core\Str::repeat(' ', $reader->depth()), $node->name(), "\n";
$node = $reader->read();
}
Output
menu
file
open
save
help
about

Before and after the walk

<?nvs
// Shows the depth before the first `read`, during the walk and after the end of the document.
Core\Xml\Reader $reader = Core\Xml::reader('<a><b><c/></b></a>');
// Nothing is read yet. This prints 0.
echo $reader->depth(), "\n";
// `c` is inside `a` and `b`. This prints 0, 1 and 2.
?Core\Xml\Node $node = $reader->read();
while ($node != null) {
echo $reader->depth(), "\n";
$node = $reader->read();
}
// `read` returned `null`, so the document is finished. This prints 0.
echo $reader->depth(), "\n";
Output
0
0
1
2
0

Count the lines of each order

<?nvs
// Shows how to find where an element ends, to count the lines inside each order.
string $xml = '<orders>
<order id="1042"><line>Mug</line><line>Teapot</line></order>
<order id="1043"><gift><line>Card</line></gift></order>
<order id="1044"/>
</orders>';
class Report
{
public static function order(tainted string $id, int $lines): void
{
echo 'order ', $id, ': ', $lines, "\n";
}
}
Core\Xml\Reader $reader = Core\Xml::reader($xml);
tainted string $order = '';
int $lines = 0;
// A closing tag is not a node. An order has depth 1, so it ends when the next node has depth 1 or 0.
// This prints order 1042: 2, order 1043: 1 and order 1044: 0.
?Core\Xml\Node $node = $reader->read();
while ($node != null) {
if ($reader->depth() <= 1 && $order != '') {
Report::order($order, $lines);
$order = '';
}
if ($node->name() == 'order') {
foreach ($node->attributes() as tainted string $name => tainted string $value) {
if ($name == 'id') {
$order = $value;
}
}
$lines = 0;
} elseif ($node->name() == 'line') {
$lines = $lines + 1;
}
$node = $reader->read();
}
// The last order also ends when the document ends.
if ($order != '') {
Report::order($order, $lines);
}
Output
order 1042: 2
order 1043: 1
order 1044: 0