2015-06-10 21 views
0

我有一個小問題,我一直在努力幾天。如何在一定數量的列後分割表格

下面是輸入XML:

<Facture> 
    <Mensualite> 
     <Lines> 
      <Line> 
       <NumeroMensualite>1</NumeroMensualite> 
       <AnneeEcheance>2015</AnneeEcheance> 
       <DateEcheance>20150618</DateEcheance> 
       <Montant>163.14</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>2</NumeroMensualite> 
       <AnneeEcheance>2015</AnneeEcheance> 
       <DateEcheance>20150718</DateEcheance> 
       <Montant>120</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>3</NumeroMensualite> 
       <AnneeEcheance>2015</AnneeEcheance> 
       <DateEcheance>20150818</DateEcheance> 
       <Montant>120</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>4</NumeroMensualite> 
       <AnneeEcheance>2015</AnneeEcheance> 
       <DateEcheance>20150918</DateEcheance> 
       <Montant>120</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>5</NumeroMensualite> 
       <AnneeEcheance>2016</AnneeEcheance> 
       <DateEcheance>20151018</DateEcheance> 
       <Montant>120</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>6</NumeroMensualite> 
       <AnneeEcheance>2016</AnneeEcheance> 
       <DateEcheance>20151118</DateEcheance> 
       <Montant>120</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>6</NumeroMensualite> 
       <AnneeEcheance>2016</AnneeEcheance> 
       <DateEcheance>20151118</DateEcheance> 
       <Montant>120</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>6</NumeroMensualite> 
       <AnneeEcheance>2017</AnneeEcheance> 
       <DateEcheance>20151113</DateEcheance> 
       <Montant>122</Montant> 
      </Line> 
      <Line> 
       <NumeroMensualite>6</NumeroMensualite> 
       <AnneeEcheance>2017</AnneeEcheance> 
       <DateEcheance>20151112</DateEcheance> 
       <Montant>124</Montant> 
      </Line> 
     </Lines> 
    </Mensualite> 
</Facture> 

這裏是我創建了XSLFO:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:set="http://exslt.org/sets" xmlns:exsl="http://exslt.org/common" xmlns:java="http://xml.apache.org/xalan/java"> 



<xsl:key name="years" match="Mensualite/Lines/Line" use="AnneeEcheance" /> 


<!-- Start --> 
<xsl:template match="/"> 
    <xsl:apply-templates select="exsl:node-set(document(/*/Document))/Facture"/> 
</xsl:template> 
<xsl:template match="Facture"> 
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
     <!--Page definitions--> 
     <fo:layout-master-set> 
      <fo:simple-page-master master-name="firstPageMaster" page-height="29.7cm" page-width="21cm" margin-top="7mm" margin-bottom="8mm" margin-left="8mm" margin-right="0mm"> 
       <fo:region-body margin-top="23mm" extent="297mm" margin-left="8mm" margin-right="8mm" region-name="body"/> 
       <fo:region-before extent="23mm" region-name="header1"/> 
      </fo:simple-page-master> 
      <fo:simple-page-master master-name="secondPageMaster" page-height="29.7cm" page-width="21cm" margin-top="7mm" margin-bottom="8mm" margin-left="8mm" margin-right="0mm"> 
       <fo:region-body margin-top="26mm" margin-bottom="95mm" extent="255mm" margin-left="8mm" margin-right="8mm" region-name="body"/> 
       <fo:region-before extent="23mm" region-name="header"/> 
      </fo:simple-page-master> 
      <fo:simple-page-master master-name="nextPagesMaster" page-height="29.7cm" page-width="21cm" margin-top="7mm" margin-bottom="8mm" margin-left="8mm" margin-right="0mm"> 
       <fo:region-body margin-top="26mm" extent="255mm" margin-left="8mm" margin-right="8mm" region-name="body"/> 
       <fo:region-before extent="23mm" region-name="header"/> 
      </fo:simple-page-master> 
      <fo:page-sequence-master master-name="firstSecondAndRestPages"> 
         <fo:single-page-master-reference master-reference="firstPageMaster"/> 
         <fo:repeatable-page-master-reference master-reference="nextPagesMaster" maximum-repeats="no-limit"/> 
      </fo:page-sequence-master> 
     </fo:layout-master-set> 

     <fo:page-sequence master-reference="firstSecondAndRestPages" font-family="Neo Sans Std" font-size="8.5pt" color="&darkGreen;" force-page-count="no-force" initial-page-number="1"> 
      <fo:flow flow-name="body"> 
      <fo:block-container position="fixed" left="10mm" top="86mm" height="30mm" width="186mm" font-size="9pt" color="black" display-align="center"> 
       <fo:table table-layout="fixed" font-size="9pt" width="186mm" space-before="3mm" space-after="1mm"> 
       <fo:table-column column-width="30mm" /> 
        <xsl:if test="count(Mensualite/Lines/Line) > 0"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 1"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 2"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 3"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 4"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 5"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 6"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 7"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 8"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 9"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 10"><fo:table-column column-width="10mm"/></xsl:if> 
        <xsl:if test="count(Mensualite/Lines/Line) > 11"><fo:table-column column-width="10mm"/></xsl:if> 
       <fo:table-body> 
       <fo:table-row font-weight="600" text-align="center"> 
        <fo:table-cell border-bottom="thin solid &darkGreen;" border-right="thin solid &darkGreen;"><fo:block>&nbsp;</fo:block></fo:table-cell> 
        <xsl:for-each select="Mensualite/Lines/Line[generate-id(.)=generate-id(key('years', AnneeEcheance)[1]) ]"> 
        <fo:table-cell border-top="thin solid &darkGreen;" border-right="thin solid &darkGreen;" number-columns-spanned="{count(key('years', AnneeEcheance))}"><fo:block><xsl:value-of select="AnneeEcheance"/></fo:block></fo:table-cell> 
        </xsl:for-each> 
       </fo:table-row> 
       <fo:table-row border-bottom="thin solid &darkGreen;" border-top="thin solid &darkGreen;" border-left="thin solid &darkGreen;" border-right="thin solid &darkGreen;"> 
       <fo:table-cell border-right="thin solid &darkGreen;" font-weight="600"><fo:block>&nbsp;Mensualité</fo:block></fo:table-cell> 
       <xsl:for-each select="key('years', Mensualite/Lines/Line/AnneeEcheance)"> 
       <fo:table-cell border-right="thin solid &darkGreen;"><fo:block><xsl:value-of select="NumeroMensualite"/></fo:block></fo:table-cell> 
       </xsl:for-each> 
       </fo:table-row> 
       <fo:table-row border-bottom="thin solid &darkGreen;" border-top="thin solid &darkGreen;" border-left="thin solid &darkGreen;" border-right="thin solid &darkGreen;"> 
       <fo:table-cell border-right="thin solid &darkGreen;" font-weight="600"><fo:block>&nbsp;Échéance</fo:block></fo:table-cell> 
       <xsl:for-each select="key('years', Mensualite/Lines/Line/AnneeEcheance)"> 
       <fo:table-cell border-right="thin solid &darkGreen;"><fo:block><xsl:value-of select="concat(substring(DateEcheance,7,2),'/',substring(DateEcheance,5,2))"/></fo:block></fo:table-cell> 
       </xsl:for-each> 
       </fo:table-row> 
       <fo:table-row border-bottom="thin solid &darkGreen;" border-top="thin solid &darkGreen;" border-left="thin solid &darkGreen;" border-right="thin solid &darkGreen;"> 
       <fo:table-cell border-right="thin solid &darkGreen;" font-weight="600"><fo:block>&nbsp;Montant (€)</fo:block></fo:table-cell> 
       <xsl:for-each select="key('years', Mensualite/Lines/Line/AnneeEcheance)"> 
       <fo:table-cell border-right="thin solid &darkGreen;"><fo:block><xsl:value-of select="Montant"/></fo:block></fo:table-cell> 
       </xsl:for-each> 
       </fo:table-row> 
       </fo:table-body> 
       </fo:table> 
       <fo:block/> 
      </fo:block-container> 
      </fo:flow> 
     </fo:page-sequence> 
    </fo:root> 
</xsl:template> 

我需要,如果XML包含更多然後拆分表12行(在這種情況下的列)並在之後創建一個新表。 我有點卡住了,它真的令人沮喪。有沒有人有任何指針?

+0

如何當每行包含4個項目時,是否可以列出一列?我建議你發佈轉換的預期結果(最好簡單地簡化爲table/s,沒有格式化噪聲)。 –

+0

爲每個Mensualite/Lines/Line列添加一個列。在一個表格中只允許有12列,所以如果有多於12個Mensualite/Lines/Line,則必須在其下面創建一個新表格。 – Mickey

回答

0

我無法理解您的XSLT。

爲了簡化問題,以每N個行,其中每行形成一列建立一個單獨的表中的問題,考慮下面的樣式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:variable name="col-limit" select="5" /> 
<xsl:variable name="rows" select="/Facture/Mensualite/Lines/Line[1]/*" /> 

<xsl:template match="/Facture"> 
    <root> 
     <xsl:apply-templates select="Mensualite/Lines/Line[position() mod $col-limit = 1]" /> 
    </root> 
</xsl:template> 

<xsl:template match="Line"> 
    <xsl:variable name="cols" select=". | following-sibling::Line[position() &lt; $col-limit]"/> 
    <table border="1"> 
     <xsl:for-each select="$rows"> 
     <xsl:variable name="row" select="position()"/> 
      <tr> 
       <th><xsl:value-of select="name()"/></th> 
       <xsl:apply-templates select="$cols/*[$row]"/> 
      </tr> 
     </xsl:for-each> 
    </table> 
</xsl:template> 

<xsl:template match="Line/*"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 

</xsl:stylesheet> 

當應用於你的例子輸入,結果(呈現)將是:

enter image description here

+0

我會試試這個,看起來很有前途,謝謝。 – Mickey