How to split table after certain number of columns

1.4k views Asked by At

I have a little issue that I've been struggling with for a couple of days now.

Here is input 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>

And here is the xslfo that i've created:

<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>

I need to split the table if the xml contains more then 12 Lines (columns in that case) and make a new table after. I'm a bit stuck on it and it's really frustrating. Does anyone have any pointers?

1

There are 1 answers

0
michael.hor257k On BEST ANSWER

I couldn't understand your XSLT.

To simplify the matter to the question of creating a separate table for every N lines, where each line forms a column, consider the following stylesheet:

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>

When applied to your example input, the result (rendered) will be:

enter image description here