This example demonstrates advanced usage canonicalizing and filtering the nodes by an XPath query.
<?php
$dom = new DOMDocument();
$dom->loadXML(<<<XML
<root xmlns:food="urn:food">
<!-- redundant namespace declaration will be canonicalized -->
<food:fruit xmlns:food="urn:food">Apple</food:fruit>
<food:fruit>Orange</food:fruit>
<food:fruit>Pear</food:fruit>
<!-- vegetables here -->
<food:vegetable>Lettuce</food:vegetable>
</root>
XML);
echo $dom->C14N(true, false, [
"query" => ".//f:fruit|.//f:fruit/text()",
"namespaces" => ["f" => "urn:food"],
]);
?>
Поданий вище приклад
виведе:
<food:fruit>Apple</food:fruit><food:fruit>Orange</food:fruit><food:fruit>Pear</food:fruit>