2017-02-10 93 views
2

必须在短消息中保留换行符。必须在网页浏览器中可见。请帮忙。我的目标是在浏览器中打开“sms.xml”,看看在不同的行使用xsl在html中显示强制换行符

“行1”和“2号线”(从邮件正文)我有两个文件:

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="" 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> 
+0

如果您在浏览器中进行转换,则仅限于XSLT 1.0。在这种情况下,请查看此问题,该问题向您展示了如何在XSLT 1.0中用'
'标签替换' '。 http://stackoverflow.com/questions/3309746/how-to-convert-newline-into-br-with-xslt –

回答

0

在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"/> 
+0

tokenize函数用于xslt 2.0,更改您的xslt版本顶部 – Rupesh

+0

谢谢亲切 –