Skip to content

Instantly share code, notes, and snippets.

@hubgit hubgit/doctype-test.php
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.