2015-06-10 1 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 элемента? Я предлагаю вам опубликовать ожидаемый результат преобразования (желательно упростить только таблицы/с без шума форматирования). –

+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

 Смежные вопросы

  • Нет связанных вопросов^_^