XSL多列切换

时间:2019-07-15 19:50:15

标签: java xml xslt xsl-fo apache-fop

我有一个xml,其中列出了客户发出的呼叫,我需要在两列中显示该消息。根据通话次数,列表可以在单页-单列,单页-双列或多页-双列中。

我必须使用基本的apace fop,并且无法访问天线库或类似的库。

当我使用for-each时,我可以显示呼叫,但只能显示在单个列上。我无法终生获得双专栏。

我最接近的是@Eliot Kimber的以下视频,但实在无法解决

https://www.youtube.com/watch?v=x6pe7KXicpA

我尝试使用列mod,但这又只是将所有调用列在一个列中

我的XML如下所示。

<CallsMade fromNumber="987654321">
    <CallItem>
        <DialledNumber>0123456789</DialledNumber>
        <DateTime>2019-07-15 15:35:35</DateTime>
        <Duration>00:04:23</Duration>
        <Cost>$1.24</Cost>
    </CallItem>
    <CallItem>
        <DialledNumber>0123456790</DialledNumber>
        <DateTime>2019-07-15 15:36:35</DateTime>
        <Duration>00:04:24</Duration>
        <Cost>$1.25</Cost>
    </CallItem>
    <CallItem>
        <DialledNumber>0123456791</DialledNumber>
        <DateTime>2019-07-15 15:37:35</DateTime>
        <Duration>>00:04:25</Duration>
        <Cost>$1.26</Cost>
    </CallItem>
    <CallItem>
        <DialledNumber>0123456792</DialledNumber>
        <DateTime>2019-07-15 15:38:35</DateTime>
        <Duration>>00:04:26</Duration>
        <Cost>$1.27</Cost>
    </CallItem>
    <CallItem>
        <DialledNumber>0123456793</DialledNumber>
        <DateTime>2019-07-15 15:39:35</DateTime>
        <Duration>>00:04:27</Duration>
        <Cost>$1.28</Cost>
    </CallItem>
    <CallItem>
        <DialledNumber>0123456794</DialledNumber>
        <DateTime>2019-07-15 15:40:35</DateTime>
        <Duration>>00:04:28</Duration>
        <Cost>$1.29</Cost>
    </CallItem>
    <CallItem>
        <DialledNumber>0123456795</DialledNumber>
        <DateTime>2019-07-15 15:41:35</DateTime>
        <Duration>>00:04:29</Duration>
        <Cost>$1.30</Cost>
    </CallItem>
    <CallItem>
        <DialledNumber>0123456931</DialledNumber>
        <DateTime>2019-07-15 17:57:34</DateTime>
        <Duration>00:06:45</Duration>
        <Cost>$2.66</Cost>
    </CallItem>
<CallsMade>`

多页的XSL,但下方是一列

<fo:block-container start-indent="0mm" left="5mm" width="48%" span="all">
        <fo:table table-layout="fixed" width="100%" font-size="5pt" text-align="center" display-align="center" span="all">
            <fo:table-column column-width="proportional-column-width(10)"/>
            <fo:table-column column-width="proportional-column-width(15)"/>
            <fo:table-column column-width="proportional-column-width(12)"/>
            <fo:table-column column-width="proportional-column-width(8)"/>
            <fo:table-body font-size="95%" >
                <xsl:for-each select="CallItem">
                    <fo:table-row height="4mm" text-align="center" display-align="center">
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="DialledNumber"/>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="DateTime"/>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="Duration"/>
                            </fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block>
                                <xsl:value-of select="Cost"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </xsl:for-each>
            </fo:table-body>
        </fo:table>
</fo:block-container>

我想知道是否有人可以帮助我显示如下列表

enter image description here

1 个答案:

答案 0 :(得分:1)

似乎您真的只需要知道您的CallItem少于60个即可。如果少于60,则使表跨两列,否则,使表跨一列。普通的分页将照顾将表拆分为列和页。

<xsl:template match="/">
  <fo:root xml:lang="en">
    <fo:layout-master-set>
      <fo:simple-page-master master-name="spm">
        <fo:region-body column-count="2" margin="36pt" />
      </fo:simple-page-master>
    </fo:layout-master-set>
    <fo:page-sequence master-reference="spm">
      <fo:flow flow-name="xsl-region-body">
        <xsl:apply-templates />
      </fo:flow>
    </fo:page-sequence>
  </fo:root>
</xsl:template>

<xsl:template match="CallsMade">
  <fo:block span="all">
    <xsl:value-of select="@fromNumber" />
  </fo:block>
  <fo:block-container>
    <xsl:if test="count(CallItem) &lt; 60">
      <xsl:attribute name="span">all</xsl:attribute>
    </xsl:if>
    <fo:table table-layout="fixed" width="100%" font-size="5pt" text-align="center" display-align="center" span="all">
      <fo:table-column column-width="proportional-column-width(10)"/>
      <fo:table-column column-width="proportional-column-width(15)"/>
      <fo:table-column column-width="proportional-column-width(12)"/>
      <fo:table-column column-width="proportional-column-width(8)"/>
      <fo:table-body font-size="95%" >
        <xsl:apply-templates select="CallItem" />
      </fo:table-body>
    </fo:table>
  </fo:block-container>
</xsl:template>

<xsl:template match="CallItem">
  <fo:table-row height="4mm" text-align="center" display-align="center">
    <fo:table-cell>
      <fo:block>
        <xsl:value-of select="DialledNumber"/>
      </fo:block>
    </fo:table-cell>
    <fo:table-cell>
      <fo:block>
        <xsl:value-of select="DateTime"/>
      </fo:block>
    </fo:table-cell>
    <fo:table-cell>
      <fo:block>
        <xsl:value-of select="Duration"/>
      </fo:block>
    </fo:table-cell>
    <fo:table-cell>
      <fo:block>
        <xsl:value-of select="Cost"/>
      </fo:block>
    </fo:table-cell>
  </fo:table-row>
</xsl:template>