Skip to content

Core\Xml\Node::source

$node->source(): tainted string

Returns an XML node and everything inside it as XML text.

Use it when you read a document with Core\Xml::parse and want to write the whole document, or one part of it, back out. Every element gets an end tag, so <e/> comes back as <e></e>. The characters &, < and > in text are escaped, so text never turns into markup. The result has no <?xml ...?> declaration at the start. It is tainted, because it came from outside the program.

Good to know: Core\Html::parse accepts some comments and names that XML cannot write. For such a tree, source throws a LogicError.

The examples below write a document and one element back out, catch the error for a tree XML cannot write, and copy some products out of a catalog into a new document.

Return value

Returns tainted string. A tainted string, with no XML declaration at the start. In text, &, < and > are escaped. In an attribute value, ", a tab and a line break are escaped too. When you parse the result, you get the same tree again.

Errors

  • LogicErrorThe tree contains something XML cannot write: a name that is not a valid XML name, a comment that contains -- or ends with -, a processing instruction that contains ?>, or a character XML does not allow. Only a tree from Core\Html::parse can contain these, because Core\Xml::parse does not accept them.

Examples

Write a node back out

<?nvs
// Shows how to write a parsed document, and one element of it, back out as XML text.
Core\Xml\Node $document = Core\Xml::parse('<note lang="en"><to>Ann &amp; Bob</to><empty/></note>');
// This prints the whole document. `<empty/>` is written as `<empty></empty>`,
// and `&` is escaped again as `&amp;`.
echo $document->source(), "\n";
// This prints only the first element inside `<note>`: <to>Ann &amp; Bob</to>
foreach ($document->children() as Core\Xml\Node $note) {
array<Core\Xml\Node> $parts = $note->children();
echo $parts[0]->source(), "\n";
}
Output
<note lang="en"><to>Ann &amp; Bob</to><empty></empty></note>
<to>Ann &amp; Bob</to>

A tree xml cannot write

<?nvs
// Shows the error you get for a tree that XML cannot write.
// `Core\Html::parse` accepts this comment. An XML comment cannot contain `--`.
Core\Xml\Node $page = Core\Html::parse('<p>Price <!-- old -- price --> 5 EUR</p>');
// `source` throws a `LogicError`, and this prints its message.
try {
echo $page->source(), "\n";
} catch (LogicError $error) {
echo $error->message, "\n";
}
Output
Core\Xml\Node::source(): a comment has no escape grammar, so it cannot hold `--` or end with `-`.

Copy the products in stock

<?nvs
// Shows how to make a new XML document that contains only some elements of another one.
string $catalog = '<catalog>
<product sku="A-1" stock="4"><name>Mug</name></product>
<product sku="B-2" stock="0"><name>Tea towel</name></product>
<product sku="C-3" stock="12"><name>Teapot</name></product>
</catalog>';
tainted string $inStock = '';
foreach (Core\Xml::parse($catalog)->children() as Core\Xml\Node $root) {
foreach ($root->children() as Core\Xml\Node $product) {
if ($product->kind() != Core\Xml\Node::Kind::Element) {
continue;
}
// A product in stock is copied as it is, with its attributes and its children.
if ($product->attributes()['stock'] != '0') {
$inStock = $inStock . $product->source();
}
}
}
// This prints a new catalog with only the mug and the teapot.
echo '<catalog>', $inStock, "</catalog>\n";
Output
<catalog><product sku="A-1" stock="4"><name>Mug</name></product><product sku="C-3" stock="12"><name>Teapot</name></product></catalog>