2016-11-10 57 views
0

我有一些XML數據和一個XSL FO樣式表來格式化XML。我有下面的XML文檔:XSL FO:在段落之間打印一條空行

<Content> 
<Para>Paragraph One.</Para> 
<Para /> 
<Para>Paragraph Two.</Para> 
<Para /> 
<Para>Paragraph Three.</Para> 
</Content> 

與FO樣式造型會後所需的輸出:

Paragraph One. 

Paragraph Two. 

Paragraph Three. 

我得到的實際產量低於總有兩個空行。

Paragraph One. 


Paragraph Two. 


Paragraph Three. 

我使用的樣式表的代碼是:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

<xsl:variable name="NewLine"> 
    <xsl:text>&#10;&#10;</text> 
</xsl:variable> 

<xsl:template match="/"> 
    <fo:root> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="pageSetup"> 
       <fo:region-body region-name="xsl-region-body" /> 
      </fo:simple-page-master> 
     </fo:layout-master-set> 

     <fo:page-sequence master-reference="pageSetup"> 
      <fo:flow flow-name="xsl-region-body"> 
       <fo:block> 
        <xsl:apply-templates /> 
       </fo:block> 
      </fo:flow> 
     </fo:page-sequence> 

    </fo:root> 
</xsl:template> 

<xsl:template match="Para"> 
    <fo:block 
     linefeed-treatment="preserve" 
     white-space-collapse="false"> 
     <xsl:choose> 
      <xsl:when test="text() != ''"> 
       <xsl:value-of select="text()" /> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$NewLine" /> 
      </xsl:otherwise> 
     </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 
+0

什麼是插入換行的意義呢?在段落之後添加額外的空白空間?如果是這樣,你應該爲你的''添加填充或邊距。 – potame

回答

0

的傳統方法來獲得空間之間的塊將使用space-beforehttps://www.w3.org/TR/xsl11/#space-before)和/或space-afterhttps://www.w3.org/TR/xsl11/#space-after)屬性。

如果你想在你的XML空Para元素堅持,你可以忽略它們,並通過更換你的模板Para使用space-after有:

<xsl:template match="Para[text()]"> 
    <fo:block space-after="1em"> 
     <xsl:apply-templates /> 
    </fo:block> 
</xsl:template>