dsgni26260
2015-09-22 14:49
浏览 72

使用XSLT和PHP双重HTML换行符

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" . "
" .
              "Content-type: text/html; charset=UTF-8" . "
";

   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?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulu8446 2015-09-23 13:59
    已采纳

    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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题