Search code examples
phphtmlxmlxsltline-breaks

Double HTML line-breaks with XSLT and PHP


I wrote this script in PHP that combines an XML document with a XSLT stylesheet, echos the result in the browser and sends it to an email address.

<?php
   $xslDoc = new DOMDocument();
   $xslDoc->load("xsltDocument.xslt");

   $xmlDoc = new DOMDocument();
   $xmlDoc->load("xmlDocument.xml");

   $proc = new XSLTProcessor();
   $proc->importStylesheet($xslDoc);
   $email = $proc->transformToXML($xmlDoc);
   echo $email;

   $headers = "MIME-Version: 1.0" . "\r\n" .
              "Content-type: text/html; charset=UTF-8" . "\r\n";

   mail("email@gmail.com","Watches", $email, $headers);
?>

It all works as expected, except for the HTML line-break elements, <br/>, which gets doubled. In the XSLT sheet, the line breaks are written a <br />, but when I check the source code for the resulting HTML output by PHP, the line breaks are written as <br></br>. This causes two line breaks where there is supposeed to be one.

The same happens in the email and there is also a problem with how tables are being displayed.

I can't post the full XML and XSLT stylesheet, but here is some of it.

xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:ms="urn:schemas-microsoft-com:xslt"
                xmlns:dt="urn:schemas-microsoft-com:datatypes"
                xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
                exclude-result-prefixes="msxsl ms dt i">
    <xsl:output
     encoding="UTF-8"
     method="html"
     indent="yes"
     doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
     doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
     media-type="application/xhtml+xml"/>

    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <!-- NAME: 1:2:1 COLUMN -->
            <meta name="viewport" content="width=device-width"></meta>
            <meta http-equiv="X-UA-Compatible" content="IE=edge"></meta>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>

xml:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xsltDocument.xslt"?>

Any ideas why this is happening?


Solution

  • I ended up setting method="xml"instead of method="html" on <xml output/> which made the resulting output by PHP render correctly in the browser and in the email.