Skip to content

Instantly share code, notes, and snippets.

@bzerangue
Created August 29, 2012 20:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bzerangue/3518650 to your computer and use it in GitHub Desktop.
Save bzerangue/3518650 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;
?>
@alixaxel
Copy link

Why / where would this be useful?

@adrianb93
Copy link

Thanks, works great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment