from different documents! DOMTemplateNodes _must_ come from DOMDocument kept privately inside DOMTemplate
public __construct ( $DOMNode, $namespaces = [] ) |
public function __construct($DOMNode, $namespaces = array()) { //we insert the templated item after the reference node, //which will always be the last item that was templated $this->refNode = $DOMNode; //take a copy of the original node that we will use as a starting point each time we iterate $this->template = $DOMNode->cloneNode(true); //initialise the template with the current, original node parent::__construct($DOMNode, $namespaces); }
public function __construct($source, $namespaces = array()) { //detect the content type; HTML or XML. HTML will need filtering during input and output //does this source have an XML prolog? $this->type = substr_compare($source, '<?xml', 0, 4, true) === 0 ? self::XML : self::HTML; //load the template file to work with, //it _must_ have only one root (wrapping) element; e.g. `<html>` $this->DOMDocument = new \DOMDocument(); if (!$this->DOMDocument->loadXML($this->type == self::HTML ? "<?xml version=\"1.0\" encoding=\"utf-8\"?>" . self::toXML($source) : $source, @LIBXML_COMPACT || @LIBXML_NONET)) { trigger_error("Source is invalid XML", E_USER_ERROR); } //set the root node for all XPath searching //(handled all internally by `DOMTemplateNode`) parent::__construct($this->DOMDocument->documentElement, $namespaces); }