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 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题