使用XSLT和PHP双重HTML换行符

时间:2022-03-14 18:17:33

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编写了这个脚本,它将XML文档与XSLT样式表结合起来,在浏览器中回显结果并将其发送到电子邮件地址。

<?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.

这一切都按预期工作,除了HTML换行元素,它加倍。在XSLT表中,换行符写为
,但是当我通过PHP检查生成的HTML输出的源代码时,换行符写为
。这导致两个换行符,其中假设为一个换行符。

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.

我无法发布完整的XML和XSLT样式表,但这里有一些。

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 个解决方案

#1


0  

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.

我最终在 上设置method =“xml”而不是method =“html”,这使得PHP的结果输出在浏览器和电子邮件中正确呈现。

#1


0  

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.

我最终在 上设置method =“xml”而不是method =“html”,这使得PHP的结果输出在浏览器和电子邮件中正确呈现。