Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
Demo script showing separation of doctype from source document during XSLTProcessor::transformToDoc
// prepare the XSL processor
$stylesheet = new DOMDocument;
$stylesheet->loadXML('<xsl:stylesheet xmlns:xsl="" version="1.0"/>');
$processor = new XSLTProcessor;
// load the XML (with a doctype)
$doc = new DOMDocument;
$doc->loadXML('<?xml version="1.0"?>
<!DOCTYPE article PUBLIC "-//example//EN" "">
printf("Doctype ID: %s\n", $doc->doctype->publicId); // Doctype ID: -//example//EN
printf("Next sibling: %s\n\n", $doc->doctype->nextSibling->nodeName); // 'article'
// write out the original document (has doctype)
print $doc->saveXML();
<?xml version="1.0"?>
<!DOCTYPE article PUBLIC "-//example//EN" "">
// transform the document (transformToDoc or transformToXML)
// this is where the doctype gets unexpectedly separated
// normally $output would be used, but is ignored here
$output = $processor->transformToDoc($doc);
// the doctype is still there, but has no nextSibling
printf("\nDoctype ID: %s\n\n", $doc->doctype->publicId); // Doctype ID: -//example//EN
//printf("Next sibling: %s\n", $doc->doctype->nextSibling->nodeName); // no nextSibling
// write out the original document again (no doctype)
print $doc->saveXML();
<?xml version="1.0"?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment