Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/9669288 to your computer and use it in GitHub Desktop.
Save hubgit/9669288 to your computer and use it in GitHub Desktop.
Demo script showing separation of doctype from source document during XSLTProcessor::transformToDoc
<?php
// prepare the XSL processor
$stylesheet = new DOMDocument;
$stylesheet->loadXML('<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"/>');
$processor = new XSLTProcessor;
$processor->importStylesheet($stylesheet);
// load the XML (with a doctype)
$doc = new DOMDocument;
$doc->loadXML('<?xml version="1.0"?>
<!DOCTYPE article PUBLIC "-//example//EN" "http://example.org/dtd">
<article/>');
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" "http://example.org/dtd">
<article/>
*/
// 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"?>
<article/>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment