如何在一定数量的列

时间:2015-06-10 08:43:20

标签: xslt-1.0 xsl-fo

我有一个小问题,我现在已经苦苦挣扎了几天。

这是输入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行(在这种情况下为列)并且之后创建一个新表,我需要拆分表。 我有点卡在上面,这真的令人沮丧。有没有人有任何指示?

1 个答案:

答案 0 :(得分:1)

我无法理解你的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

相关问题