首页 > 解决方案 > 可以以 XML 格式发送电子邮件的 Python 脚本

问题描述

是否可以通过使用 XML 文档在电子邮件正文中创建可以通过电子邮件发送 XML 分层格式的 Python 脚本。

只是为您提供背景知识,我在 XML 文档中更新了一些细节,并希望在电子邮件中输入相同的 XML 格式。

目前,我使用 XSL 来设置 XML 文档的样式,并使用 Python“lxml”库将 XML 转换为 HTML,但我当前的脚本正在删除最终输出中的 XML 标记。

因此,我正在寻找一种在我的输出中保留这些 XML 标记的方法,我最好使用 XML 到 HTML 的相同方法,因为我需要在我的自动电子邮件中加载这个 HTML 结构。

Python 脚本

from lxml import etree

xsl = etree.parse("tx.xsl")
updated = etree.XSLT(xsl)
xml = etree.parse("tx.xml")
html = updated(xml)

html.write("XML-Transformed.html")

XSL 代码

?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
        <html>
            <body>
                <table>
                    <xsl:for-each select="ROOT">
                           <tr>
                             <td><xsl:value-of select="Summary"/></td>
                           </tr>
                           <tr>
                             <td><xsl:value-of select="customer_Name"/></td>
                           </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
     </xsl:template>
 </xsl:stylesheet>

XML

<ROOT>
<Summary>ZZZZ</Summary>
<customer_Name>YYYY</customer_Name>
</ROOT>

电流输出

ZZZZ
YYYY

预期的

<Summary>ZZZZ</Summary>
<customer_Name>YYYY</customer_Name>

标签: pythonxmlxsltlxml

解决方案


XSLT 转换的工作方式是,您需要明确地将输出中所需的所有内容添加到转换中。此外,如果您希望浏览器显示这些 XML 标记,则需要对它们进行转义

试试这个 XSLT 文件,在浏览器中打开结果,让我知道它是否是您需要的:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
        <html>
            <body>
                <table>
                    <xsl:for-each select="ROOT">
                           <tr>
                             <td>&lt;Summary&gt;<xsl:value-of select="Summary"/>&lt;/Summary&gt;</td>
                           </tr>
                           <tr>
                             <td>&lt;customer_Name&gt;<xsl:value-of select="customer_Name"/>&lt;/customer_Name&gt;</td>
                           </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
     </xsl:template>
 </xsl:stylesheet>

编辑:如果您想要带有自定义标签的 HTML,您需要将它们显式添加到 XSLT 转换中 - 这次不要转义:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:template match="/">
        <html>
            <body>
                <table>
                    <xsl:for-each select="ROOT">
                           <tr>
                             <td><Summary><xsl:value-of select="Summary"/></Summary></td>
                           </tr>
                           <tr>
                             <td><customer_Name><xsl:value-of select="customer_Name"/></customer_Name></td>
                           </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
     </xsl:template>
 </xsl:stylesheet>

推荐阅读