[英]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.