使用 xsl 在 html 中显示的 xml 中强制换行

force line break in xml displayed in html using xsl

必须保留短信中的换行符。必须在网络浏览器中可见。请帮忙。我的目标是在浏览器中打开 "sms.xml" 并在单独的行中查看 "line1" 和 "line2"(来自邮件正文)

我有两个文件:

sms.xml:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<?xml-stylesheet type="text/xsl" href="sms.xsl"?>
<smses count="1" backup_set="b8123816-3935-4dee-9705-4b484ebe0582" backup_date="1486606490878">
  <sms protocol="0" address="0123456789" date="0000000000000" type="2" subject="null" body="line1&#10;line2" toa="null" sc_toa="null" service_center="null" read="1" status="-1" locked="0" date_sent="0" readable_date="Aug 19, 1949 10:53:27 AM" contact_name="anonymous" />
</smses>

sms.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="utf-8" indent="yes" />
    <xsl:template match="/">
        <xsl:text disable-output-escaping="yes" />
        <html>
            <head>
                <style type="text/css">
                    body{
                        font-family:arial,sans-serif;
                        color:#000;
                        font-size:13px;
                        color:#333;
                    }
                    table{
                        font-size:1em;
                        margin:0 0 1em;
                        border-collapse:collapse;
                        border-width:0;
                        empty-cells:show;
                    }
                    td,th{
                        border:1px solid #ccc;
                        padding:6px 12px;
                        text-align:left;
                        vertical-align:top;
                        background-color:inherit;
                    }
                    th{
                        background-color:#dee8f1;
                    }
                </style>
            </head>
            <body>
                <h2>SMS &amp; MMS Messages</h2>
                <table>
                    <colgroup>
                        <col style="width:80px"/>
                        <col style="width:120px"/>
                        <col style="width:120px"/>
                        <col style="width:180px"/>
                    </colgroup>
                    <tr>
                        <th>Type</th>
                        <th>Number</th>
                        <th>Contact</th>
                        <th>Date</th>
                        <th>Message</th>
                    </tr>
                    <xsl:for-each select="smses/*">
                        <tr>
                            <xsl:choose>
                                <xsl:when test="name() = 'sms'">
                                    <td>
                                        <xsl:if test="@type = 1">Received</xsl:if>
                                        <xsl:if test="@type = 2">Sent</xsl:if>
                                        <xsl:if test="@type = 3">Draft</xsl:if>
                                    </td>
                                </xsl:when>
                                <xsl:otherwise>
                                    <td>
                                        <xsl:if test="@msg_box = 1">Received</xsl:if>
                                        <xsl:if test="@msg_box = 2">Sent</xsl:if>
                                        <xsl:if test="@msg_box = 3">Draft</xsl:if>
                                    </td>
                                </xsl:otherwise>
                            </xsl:choose>
                            <td><xsl:value-of select="@address"/></td>
                            <td><xsl:value-of select="@contact_name"/></td>
                            <td><xsl:value-of select="@readable_date"/></td>
                            <td>
                                <xsl:choose>
                                    <xsl:when test="name() = 'sms'">
                                        <xsl:value-of select="@body"/>
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:for-each select="parts/part">
                                            <xsl:choose>
                                                <xsl:when test="@ct = 'application/smil'">
                                                </xsl:when>
                                                <xsl:when test="@ct = 'text/plain'">
                                                    <xsl:value-of select="@text"/><br/>
                                                </xsl:when>
                                                <xsl:when test="starts-with(@ct,'image/')" >
                                                    <img height="300">
                                                        <xsl:attribute name="src">
                                                            <xsl:value-of select="concat(concat('data:',@ct), concat(';base64,',@data))"/>
                                                        </xsl:attribute>
                                                    </img><br/>
                                                </xsl:when>
                                                <xsl:otherwise>
                                                    <i>Preview of <xsl:value-of select="@ct"/> not supported.</i><br/>
                                                </xsl:otherwise>
                                            </xsl:choose>
                                        </xsl:for-each>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

在 xslt 2.0 中你可以使用

<xsl:for-each select="tokenize(@body, '&#10;')">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()"><br/></xsl:if>
 </xsl:for-each>

而不是

<xsl:value-of select="@body"/>