Skip to content

Core\Xml\Node::Kind

What a node in a parsed document is: the document itself, an element, some text, a comment or a processing instruction. There are five and there will never be a sixth, so a walk that handles all five has handled everything a document can hold.

Core\Xml\Node::Kind is declared in the class Core\Xml\Node. The method kind of a node returns it.

Reading HTML and reading XML answer the same five kinds, so a walk written for one works unchanged on the other. Every node answers which kind it is, and that answer is the first question a walk asks, before it reaches for a name, a piece of text or a list of children.

Good to know: the children of a tag are not all tags. In a file somebody indented by hand, the line breaks between the tags are text nodes of their own, and a note left in the markup is a comment node sitting among them. An element also carries no characters itself: the words it wraps are in the text node underneath it.

Examples

The five things a document is made of

<?nvs
// Every node in a parsed XML document is one of five kinds.
// This program prints the kind of each node in a small document.
class Walk
{
public static function name(Core\Xml\Node::Kind $kind): string
{
return match ($kind) {
Core\Xml\Node::Kind::Document => 'document',
Core\Xml\Node::Kind::Element => 'element',
Core\Xml\Node::Kind::Text => 'text',
Core\Xml\Node::Kind::Comment => 'comment',
Core\Xml\Node::Kind::ProcessingInstruction => 'instruction',
};
}
public static function show(Core\Xml\Node $node, string $indent): void
{
var $kind = $node->kind();
echo $indent, Walk::name($kind);
// An element and an instruction have a name, so this prints it.
// Text, a comment and an instruction have content, so this prints that too.
if ($kind == Core\Xml\Node::Kind::Element || $kind == Core\Xml\Node::Kind::ProcessingInstruction) {
echo ' ', $node->name();
}
if ($kind != Core\Xml\Node::Kind::Element && $kind != Core\Xml\Node::Kind::Document) {
echo ': ', $node->text();
}
echo "\n";
foreach ($node->children() as Core\Xml\Node $child) {
Walk::show($child, $indent . ' ');
}
}
}
var $doc = Core\Xml::parse("<?xml version=\"1.0\"?><?render mode=\"draft\"?><order id=\"1042\"><!-- from the shop --><item>Coffee beans</item></order>");
Walk::show($doc, '');
Output
document
instruction render: mode="draft"
element order
comment: from the shop
element item
text: Coffee beans

Reading only the words a document says

<?nvs
// This program collects the text of an XML document.
// It checks the kind of each node. It keeps text nodes and skips comments
// and processing instructions.
class Reader
{
public static function words(Core\Xml\Node $node): tainted string
{
if ($node->kind() == Core\Xml\Node::Kind::Text) {
return $node->text();
}
// A comment or an instruction gives an empty string.
// An element or the document gives the text of all its children.
if (
$node->kind() == Core\Xml\Node::Kind::Comment
|| $node->kind() == Core\Xml\Node::Kind::ProcessingInstruction
) {
return '';
}
tainted string $said = '';
foreach ($node->children() as Core\Xml\Node $child) {
$said = $said . Reader::words($child);
}
return $said;
}
}
var $doc = Core\Xml::parse('<page><?render draft?><h1>Opening hours</h1><!-- check before Easter --> <p>Every day from <b>9</b> until <b>17</b>.</p></page>');
// The space between the comment and `<p>` is a text node too,
// so it is part of the result.
echo Reader::words($doc), "\n";
Output
Opening hours Every day from 9 until 17.

Reading a price list a supplier sent

<?nvs
// Reading a price list a supplier sent. A file written by hand is indented, so
// the children of a tag are not all tags: the line breaks between them are text
// nodes, and the note somebody left is a comment node. Asking each child its
// kind is what keeps them out of the count.
class Feed
{
// An element carries no characters of its own. The words a tag wraps are
// in the text node underneath it, which is why this looks one level down.
public static function label(Core\Xml\Node $element): tainted string
{
tainted string $words = '';
foreach ($element->children() as Core\Xml\Node $child) {
if ($child->kind() == Core\Xml\Node::Kind::Text) {
$words = $words . $child->text();
}
}
return $words;
}
}
var $xml = "<prices>\n <item sku=\"CB-100\">Coffee beans</item>\n <!-- winter price, review in March -->\n <item sku=\"TL-220\">Tea leaves</item>\n</prices>";
var $doc = Core\Xml::parse($xml);
uint $children = 0;
uint $items = 0;
uint $notes = 0;
foreach ($doc->children() as Core\Xml\Node $prices) {
foreach ($prices->children() as Core\Xml\Node $child) {
$children = $children + 1;
if ($child->kind() == Core\Xml\Node::Kind::Comment) {
$notes = $notes + 1;
echo 'note: ', Core\Str::trim($child->text()), "\n";
continue;
}
if ($child->kind() != Core\Xml\Node::Kind::Element) {
continue;
}
$items = $items + 1;
echo 'item: ', Feed::label($child);
foreach ($child->attributes() as tainted string $name => tainted string $value) {
echo ' (', $name, ' ', $value, ')';
}
echo "\n";
}
}
echo "\n", $children, ' children of <prices>, of which ', $items, ' are products and ',
$notes, " a note somebody left\n";
Output
item: Coffee beans (sku CB-100)
note: winter price, review in March
item: Tea leaves (sku TL-220)
7 children of <prices>, of which 2 are products and 1 a note somebody left