Skip to content

Instantly share code, notes, and snippets.

@bateller
Forked from bzerangue/transform-html5.php
Created August 12, 2016 13:13
Show Gist options
  • Save bateller/23d5dd99236b8c83d798e7a90eff5c31 to your computer and use it in GitHub Desktop.
Save bateller/23d5dd99236b8c83d798e7a90eff5c31 to your computer and use it in GitHub Desktop.
PHP: XSLT Transformation to build HTML5 html output (use xhtml 1.0 strict as your settings in your XSLT stylesheet)
<?php
// Load the XML source
$xml = new DOMDocument;
$xml->load('XML_SOURCE_LINK_HERE');
// Load XSLT stylesheet
$xsl = new DOMDocument;
$xsl->load('XSL_STYLESHEET_PAGE_LINK_HERE');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
$transformed = $proc->transformToXML($xml);
// determining if output is html document
$html = $transformed;
// splitting up html document at doctype and doc
$html_array = explode("\n",$html,15);
$html_doc = array_pop($html_array);
$html_doctype = implode("\n",$html_array);
// convert XHTML syntax to HTML5
// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
// <!DOCTYPE html>
$html_doctype = preg_replace("/<!DOCTYPE [^>]+>/", "<!DOCTYPE html>", $html_doctype);
// <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
// <html lang="en">
$html_doctype = preg_replace('/ xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"| xml:lang="[^\"]*\"/', '', $html_doctype);
// <meta http-equiv="content-type" content="text/html; charset=utf-8" />
// to this --> <meta charset="utf-8" />
$html_doctype = preg_replace('/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=(.*[a-z0-9-])\" \/>/i', '<meta charset="\1" />', $html_doctype);
$html = $html_doctype . "\n" . $html_doc;
echo $html;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment