[英]How to display invisible linebreaks in XML using XSLT1.0
这就是我的XML输出中显示的内容(请注意换行符)
<root>
<Item type="Comment">
<comment>this is a test
this is a test
this is a test</comment>
</Item>
</root>
在我的样式表中
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:user-scripts">
<xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" cdata-section-elements="script msxsl:script"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/Root">
<xsl:apply-templates select="Item"/>
</xsl:template>
<xsl:template match="Item[@type='Comment']">
<html>
<head>
<title>
Comment: <xsl:value-of select="comment_no"/>
</title>
</head>
<style>
* {
font-family: "Arial", sans-serif;
font-size: 10pt;
}
body {
min-width: 775px;
max-width: 775px;
margin-left: auto;
margin-right: auto;
}
td {
padding: 0px;
}
.layoutTable {
width: 100%;
}
.fieldValue {
width: 99%;
padding-left: 7px;
}
.layoutTable {
width: 100%;
}
div.layoutBin {
page-break-inside: avoid;
overflow: hidden;
border: solid;
border-width: 1px;
margin-bottom: 10px;
padding-left: 3px;
padding-right: 3px;
}
</style>
<body>
<div class="layoutBin" id="keyInfo">
<div class="left" style="width: 25%">
<table class="layoutTable">
<tr>
<td class="fieldValue">
<xsl:value-of select="comment"/>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
换行符被删除,我的浏览器将结果显示在一行中。
如何保存/显示换行符?
在HTML中,换行符通常不重要。 它们与其他空白的处理方式相同-变成单个单词空间。 这样,您无法通过查看页面来判断DOM中是否存在文字换行符。
您有几种选择:
white-space: pre;
或white-space: pre-wrap;
。 (这是最简单的,因为您已经在编写自己的CSS)。 注意:这还将使您要插入的所有其他文本都有意义,因此您可能要使用xsl:text
标记。 br
标签代替换行符。 div
(或段落或类似文本) 选择哪种取决于您要对文本执行的操作。
xsl:preserve-space元素最初可能看起来就是您想要的。 将其放在要用逐个空格保留其空格的元素列表之间,并以空格分隔,将其放在样式表的顶部-但是只有在使用xsl:strip-space
才有必要,看起来好像没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.