2014-01-09 52 views
3

我正在嘗試使用xsl(FOP)將我的XML數據轉換爲表格格式,但無法完全完成,因爲單元格中沒有文本時單元格會重疊。如何在xsl中正確調整表格單元格

這裏是我的XSL代碼:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output encoding="iso-8859-1" /> 
<xsl:template match ="records"> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <fo:layout-master-set> 
     <fo:simple-page-master master-name="list"> 
      <fo:region-body margin-left="50pt" 
         margin-top="50pt" 
         margin-bottom="50pt" 
         margin-right="50pt"></fo:region-body> 
     </fo:simple-page-master> 
    </fo:layout-master-set> 

    <fo:page-sequence master-reference="list"> 
     <fo:flow flow-name="xsl-region-body"> 
      <fo:block text-align="center"> 
       <fo:table table-layout="fixed" width="100%" > 
        <fo:table-body> 
         <xsl:for-each select="./list"> 
          <xsl:if test="position()&lt;= 1"> 
           <fo:table-row> 

            <xsl:for-each select="./item"> 

            <fo:table-cell text-align="center" border="2px"> 
             <fo:block color="green" font-family="monospace" font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt"> 
             <xsl:attribute name="color"> 
            <xsl:choose> 
             <xsl:when test="position() = 1 "> 
             <xsl:text>black</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 2 "> 
             <xsl:text>#333399</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 3 "> 
             <xsl:text>#FF9900</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 4 "> 
             <xsl:text>#96CCD8</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 5 "> 
             <xsl:text>#19A347</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 6 "> 
             <xsl:text>green</xsl:text> 
             </xsl:when> 
             <xsl:otherwise> 
             <xsl:text>white</xsl:text> 
             </xsl:otherwise> 
            </xsl:choose> 
           </xsl:attribute> 
           <xsl:value-of select="val"/> 

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

            </xsl:for-each> 

           </fo:table-row> 
           </xsl:if> 
          </xsl:for-each> 

         <xsl:for-each select="./list"> 
         <xsl:if test="position()!=1"> 
          <fo:table-row> 
          <xsl:for-each select="./item"> 
          <fo:table-cell border="4px" text-align="center"> 
          <fo:block font-family="monospace" border-style="solid" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt"> 
           <xsl:attribute name="background-color"> 
            <xsl:choose> 
             <xsl:when test="position() = 1 "> 
             <xsl:text>#C1BFC4</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 2 "> 
             <xsl:text>#B1A1C8</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 3 "> 
             <xsl:text>#F9CAA0</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 4 "> 
             <xsl:text>#96CCD8</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 5 "> 
             <xsl:text>#C2D89A</xsl:text> 
             </xsl:when> 
             <xsl:when test="position() = 6 "> 
             <xsl:text>green</xsl:text> 
             </xsl:when> 
             <xsl:otherwise> 
             <xsl:text>red</xsl:text> 
             </xsl:otherwise> 
            </xsl:choose> 
           </xsl:attribute> 
           <xsl:value-of select="val"/> 
          </fo:block> 
           </fo:table-cell> 
           </xsl:for-each> 
          </fo:table-row> 
          </xsl:if> 
         </xsl:for-each> 
        </fo:table-body> 
       </fo:table> 
      </fo:block> 
     </fo:flow> 
    </fo:page-sequence> 
</fo:root> 
</xsl:template> 
</xsl:stylesheet> 

我的XML代碼:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<records> 
<list> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>Subject Number</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>Monthly Dairy Contact</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>3-Month Safety Contact</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>Annual visit</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>Suspected HZ follow-up Visit</val> 
    </item> 
</list> 
<list> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>49210</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>x</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>15-sep-2012</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val></val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val></val> 
    </item> 
</list> 
<list> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>49210</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>x</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>15-sep-2012</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>test</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val></val> 
    </item> 
</list> 
<list> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>49210</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>x</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>15-sep-2012</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>test</val> 
    </item> 
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue"> 
     <val>test</val> 
    </item> 
</list> 
</records> 

我的XSL輸出如下: enter image description here

任何一個可以修改我的代碼以便正確顯示錶格,而不像我在圖像中顯示的那樣。

回答

2

如果將border-stylebackground-color屬性從fo:block移動到fo:table-cell,它將起作用。該位代碼也可以通過刪除xsl:text元素來簡化:

<fo:table-cell border="4px" border-style="solid" text-align="center"> 
    <xsl:attribute name="background-color"> 
    <xsl:choose> 
     <xsl:when test="position() = 1">#C1BFC4</xsl:when> 
     <xsl:when test="position() = 2">#B1A1C8</xsl:when> 
     <xsl:when test="position() = 3">#F9CAA0</xsl:when> 
     <xsl:when test="position() = 4">#96CCD8</xsl:when> 
     <xsl:when test="position() = 5">#C2D89A</xsl:when> 
     <xsl:when test="position() = 6">green</xsl:when> 
     <xsl:otherwise>red</xsl:otherwise> 
    </xsl:choose> 
    </xsl:attribute> 
    <fo:block font-family="monospace" font-size="12pt" wrap-option="no-wrap" 
     padding="5pt" space-before="5pt" space-after="5pt"> 
    <xsl:value-of select="val"/> 
    </fo:block> 
</fo:table-cell> 
相關問題