剥离白色 space 似乎坏了

Stripping of white space seems broken

由于更好的 Java 兼容性,我正在将一些 XSLT 模板移动到 freemarker,但 freemarker 似乎在处理 whitespace 方面存在问题。 XML 到处都是白色 space,就像这样:

</fo:block>

</fo:table-cell>
</fo:table-row>
    <fo:table-row>  <fo:table-cell  



>   <fo:block >
    <fo:inline  font-weight="bold"

我尝试添加 strip_whitespace=true 指令,但没有效果。这是默认设置,因此它可能已经可以正常工作了。

在 XSLT 中,XML 块之间的所有白色 space 都被折叠。通过将任何自由文本需求放在 <xsl:text> example text including padding </xsl:text> 元素中,这会变得更容易。

看来freemarker对文本节点缺乏控制,没办法塌白space。

UPDATE:这里是 freemarker 模板的一部分示例,它在其中生成 fo:table-cell

<#macro tableHeaderM tableHeader>
    <#list tableHeader.columns as column><@columnM column=column /></#list>
</#macro>
<#macro columnM column>
    <fo:table-column column-width="${column.width}cm" />
</#macro>
<#macro rowM row table>
    <fo:table-row><#list row.cells as cell><@cellM cell=cell table=table /></#list></fo:table-row>
</#macro>
<#macro cellM cell table>
    <fo:table-cell<@cellFormattingM cell=cell table=table />><@blockObjectsM blockObjects=cell.blockObjects /></fo:table-cell>
</#macro>
<#macro cellFormattingM cell table>
    <#if cell.border == true> border="0.1mm solid"</#if>
    <#if cell.pad == true> padding="3pt"</#if>
    <#if cell.shade == true> background-color="#eeeeee"</#if>
    <#if table.collapse == true> margin-top="5px"</#if>
</#macro>

FreeMarker 中的 white-space 剥离和修剪仅用于删除模板本身中存在的 white-space(即,为源代码格式添加的 white-space ).您的案例中的 white-space 存在于数据模型中。不幸的是,您需要预处理 DOM 以摆脱这些(或插入 ?trim 等)。 FreeMarker DOM 包装器不知道哪个白色space 是无关紧要的。

Update(因为 white-space 不是来自 DOM): strip_whitespace(白色-space 剥离)默认为 true,因此添加不会去除更多白色-space。所以你最好的选择是使用 <#t>(也许 <#rt><#lt>)明确地删除更多的白色-space。