使用xsl强制在html中显示xml换行
必须在sms消息中保留换行符。必须在web浏览器中可见。请帮忙。我的目标是在浏览器中打开“sms.xml”,并在单独的行中查看“line1”和“line2”(来自消息正文) 我有两个文件: sms.xml:使用xsl强制在html中显示xml换行,html,xml,xslt,sms,newline,Html,Xml,Xslt,Sms,Newline,必须在sms消息中保留换行符。必须在web浏览器中可见。请帮忙。我的目标是在浏览器中打开“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_
<?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 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 & 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>
身体{
字体系列:arial,无衬线;
颜色:#000;
字体大小:13px;
颜色:#333;
}
桌子{
字号:1em;
边缘:0.01米;
边界塌陷:塌陷;
边框宽度:0;
空单元格:显示;
}
td,th{
边框:1px实心#ccc;
填充:6px 12px;
文本对齐:左对齐;
垂直对齐:顶部;
背景色:继承;
}
th{
背景色:#dee8f1;
}
短讯及;彩信
类型
数
接触
日期
消息
收到
发送
草稿
收到
发送
草稿
不支持的预览。
在XSLT2.0中,您可以使用
<xsl:for-each select="tokenize(@body, ' ')">
<xsl:value-of select="."/>
<xsl:if test="position() != last()"><br/></xsl:if>
</xsl:for-each>
而不是
<xsl:value-of select="@body"/>
tokenize函数在xslt 2.0中使用,请在topgreat更改您的xslt版本!我想知道为什么很难找到这样一个现代而优雅的答案。我希望我能投更多的票。如果你在浏览器中进行转换,你只能使用XSLT1.0。在这种情况下,检查这个问题,它向您展示了如何替换
代码>在XSLT1.0中带有
标记。