everywhere anytime
everywhere anytime

Reputation: 43

force line break in xml displayed in html using xsl

must preserve line breaks in sms messages. must be viewable in web browser. please help. my goal is to open "sms.xml" in browser and see "line1" and "line2" (from message body) on separate lines

i have two files:

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>

Upvotes: 2

Views: 892

Answers (1)

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

in xslt 2.0 you can use

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

instead of

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

Upvotes: 1

Related Questions