Core\Xml\Node::kind
$node->kind(): Core\Xml\Node::KindReturns what kind of node this is, as a Core\Xml\Node::Kind case. There are five kinds: an element, a text, a comment, a processing instruction and the document.
The node that Core\Xml::parse returns is always the document. Every node below it is one of the other four. The spaces and line breaks between two elements are text nodes too.
A program usually checks the kind before it reads anything else from a node. For example, it skips everything that is not an element, or it collects only the comments. You compare the result with a case such as Core\Xml\Node::Kind::Element, or you use match to handle all five kinds.
The examples below name the kind of every node in a document, find the comments, and get the plain text of a description written as XHTML.
Return value
Returns Core\Xml\Node::Kind. A Core\Xml\Node::Kind case. There are exactly five cases, so a match over them covers every node.
Examples
Name every kind
<?nvs// Shows what kind of node each part of a small document is.
class Label{ // This returns a short word for each of the five kinds of node. public static function of(Core\Xml\Node::Kind $kind): string { return match ($kind) { Core\Xml\Node::Kind::Element => 'element', Core\Xml\Node::Kind::Text => 'text', Core\Xml\Node::Kind::Comment => 'comment', Core\Xml\Node::Kind::ProcessingInstruction => 'processing instruction', Core\Xml\Node::Kind::Document => 'document', }; }}
Core\Xml\Node $document = Core\Xml::parse('<?style dark?><!--saved--><note>Buy milk</note>');
// `Core\Xml::parse` returns the document node. This prints document.echo Label::of($document->kind()), "\n";
// This prints processing instruction, comment and element.// The text inside the `note` element is a text node.foreach ($document->children() as Core\Xml\Node $child) { echo Label::of($child->kind()), "\n"; foreach ($child->children() as Core\Xml\Node $inside) { echo ' ', Label::of($inside->kind()), "\n"; }}documentprocessing instructioncommentelement textFind the comments
<?nvs// Shows how to find the comments in a document.
string $xml = '<settings> <!-- The port must be free. --> <port>8080</port> <!-- Use false in production. --> <debug>true</debug></settings>';
Core\Xml\Node $document = Core\Xml::parse($xml);
foreach ($document->children() as Core\Xml\Node $root) { foreach ($root->children() as Core\Xml\Node $child) { // Only a comment has the kind `Comment`. This prints the two notes. if ($child->kind() == Core\Xml\Node::Kind::Comment) { echo Core\Str::trim($child->text()), "\n"; } }}The port must be free.Use false in production.The plain text of a description
<?nvs// Shows how to get the plain text of a product description that is written as XHTML.
class PlainText{ // This joins the text of every text node below `$node`. // It skips comments and processing instructions. public static function of(Core\Xml\Node $node): tainted string { if ($node->kind() == Core\Xml\Node::Kind::Text) { return $node->text(); } tainted string $text = ''; foreach ($node->children() as Core\Xml\Node $child) { Core\Xml\Node::Kind $kind = $child->kind(); if ($kind == Core\Xml\Node::Kind::Text || $kind == Core\Xml\Node::Kind::Element) { $text = $text . PlainText::of($child); } } return $text; }}
string $description = '<p>A <b>large</b> mug<!-- check the size -->, dishwasher safe.</p>';
// This prints A large mug, dishwasher safe.echo PlainText::of(Core\Xml::parse($description)), "\n";A large mug, dishwasher safe.