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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题