如果使用XSLT将表内容扩展到PDF中的下一页,则表头不会重复

时间:2018-05-09 14:10:05

标签: xslt xslt-1.0 xslt-2.0 xsl-fo

如果表格内容扩展到下一页,则表格标题不会添加到下一页。正在为多个表显示标题,但如果表内容扩展到下一页,则标题不会出现,并且它会在下一页中查看表行。如果表内容扩展,则需要在开始下一页之前附加标题。 示例XML:

 <NewDataSet>
   <DefaultView>
     <Department>2222</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2222</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2222</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2222</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2223</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2223</Department>
     <Title>vijay</Title>
   </DefaultView>
    <DefaultView>
     <Department>2223</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2223</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2224</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2224</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2224</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2224</Department>
     <Title>vijay</Title>
   </DefaultView>
    <DefaultView>
     <Department>2224</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2224</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2224</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2225</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2225</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2225</Department>
     <Title>vijay</Title>
   </DefaultView>
    <DefaultView>
     <Department>2225</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2225</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2226</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2226</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2226</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2226</Department>
     <Title>vijay</Title>
   </DefaultView> <DefaultView>
     <Department>2226</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2226</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>vijay</Title>
   </DefaultView> <DefaultView>
     <Department>2227</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>vijay</Title>
   </DefaultView> <DefaultView>
     <Department>2227</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>vijay</Title>
   </DefaultView> <DefaultView>
     <Department>2227</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>vijay</Title>
   </DefaultView> <DefaultView>
     <Department>2227</Department>
     <Title>Manish</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Santosh</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>Naveen</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>punith</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>bharath</Title>
   </DefaultView>
   <DefaultView>
     <Department>2227</Department>
     <Title>vijay</Title>
   </DefaultView>
 </NewDataSet>

示例XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:fo="http://www.w3.org/1999/XSL/Format"
                              xmlns:rx="http://www.renderx.com/XSL/Extensions"
                              xmlns:psmi="http://www.CraneSoftwrights.com/resources/psmi"
                              xmlns:exsl="http://exslt.org/common">


  <xsl:template match='NewDataSet'>
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="my-page">
          <fo:region-body margin="1in"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="my-page">
        <fo:flow flow-name="xsl-region-body">
          <xsl:for-each select="*">

                <xsl:variable name="tempdept">
                            <xsl:value-of select="Department" />
                </xsl:variable>
                <xsl:variable name="dept">
                            <xsl:value-of select="preceding-sibling::DefaultView[1]/Department" />
                </xsl:variable>
                <xsl:if test="$dept != $tempdept" >
                        <xsl:call-template name="header"/>
                </xsl:if>
                <fo:table-and-caption>
                <fo:table rx:table-omit-initial-header="true">  
                  <fo:table-column column-width="25mm"/>
                  <fo:table-column column-width="25mm"/>
                  <fo:table-header  keep-together="always">
                            <fo:block text-align="center" font-style="italic" margin="5pt" keep-together="always">
                                <xsl:text>Table (Cont.)</xsl:text>
                            </fo:block>
                    </fo:table-header>
                  <fo:table-header >
                    <fo:table-row>
                      <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" align="left">
                        <fo:block font-weight="bold">Dept</fo:block>
                      </fo:table-cell >
                      <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" align="left">
                        <fo:block font-weight="bold">Title</fo:block>
                      </fo:table-cell>
                    </fo:table-row>
                  </fo:table-header>
                    <fo:table-body>
                      <fo:table-row>
                        <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" align="left">
                          <fo:block>
                            <xsl:value-of select="Department"/>
                          </fo:block>
                        </fo:table-cell>
                        <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" align="left">
                          <fo:block>
                            <xsl:value-of select="Title"/>
                          </fo:block>
                        </fo:table-cell>
                      </fo:table-row>
                    </fo:table-body>
                </fo:table>
              </fo:table-and-caption>
          </xsl:for-each>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template name='header'>
        <fo:table>  
              <fo:table-column column-width="25mm"/>
              <fo:table-column column-width="25mm"/>
              <fo:table-header  keep-together="always">
                        <fo:block text-align="center" font-style="italic" margin="5pt" keep-together="always">
                            <xsl:text>Table</xsl:text>
                        </fo:block>
                </fo:table-header>
                 <fo:table-header >
                <fo:table-row>
                  <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" align="left">
                    <fo:block font-weight="bold">Dept</fo:block>
                  </fo:table-cell >
                  <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" align="left">
                    <fo:block font-weight="bold">Title</fo:block>
                  </fo:table-cell>
                </fo:table-row>
              </fo:table-header>
        </fo:table>
</xsl:template>

</xsl:stylesheet>

示例O / P屏幕截图: 1.有多个表格: Table header with multiple table 2.如果表格内容扩展到下一页,则标题不会出现: Header is not coming if table content extends to next page

1 个答案:

答案 0 :(得分:1)

因为你有很多问题,所以这可能是你想要做的事情。这个XSL为每个DefaultView创建“行”,并且只有一个表具有重复的标题,在起始时仅在中断处没有表(cont)。

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        xmlns:rx="http://www.renderx.com/XSL/Extensions"
        xmlns:psmi="http://www.CraneSoftwrights.com/resources/psmi"
        xmlns:exsl="http://exslt.org/common">


        <xsl:template match='NewDataSet'>
            <fo:root>
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="my-page">
                        <fo:region-body margin="1in"/>
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="my-page">
                    <fo:flow flow-name="xsl-region-body">

                            <fo:table-and-caption>
                                <fo:table rx:table-omit-initial-header="true">  
                                    <fo:table-column column-width="25mm"/>
                                    <fo:table-column column-width="25mm"/>
                                    <fo:table-header  keep-together="always">
                                        <fo:table-row>
                                            <fo:table-cell number-columns-spanned="2"><fo:block text-align="center" font-style="italic" margin="5pt" keep-together="always">
                                                <xsl:text>Table (Cont.)</xsl:text>
                                            </fo:block>
                                        </fo:table-cell>
                                        </fo:table-row>
                                        <fo:table-row>
                                        <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" >
                                            <fo:block font-weight="bold">Department</fo:block>
                                        </fo:table-cell >
                                        <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" >
                                            <fo:block font-weight="bold">Title</fo:block>
                                        </fo:table-cell>
                                        </fo:table-row>
                                    </fo:table-header>
                                    <fo:table-body>
                                        <fo:table-row>
                                            <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" >
                                                <fo:block font-weight="bold">Depaartment</fo:block>
                                            </fo:table-cell >
                                            <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" >
                                                <fo:block font-weight="bold">Title</fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                        <xsl:apply-templates/>
                                    </fo:table-body>
                                </fo:table>
                            </fo:table-and-caption>

                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
        <xsl:template match="DefaultView">
            <fo:table-row>
                <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" >
                    <fo:block>
                        <xsl:value-of select="Department"/>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell border-color="black" border-style="solid" width = "85pt" border-width="0.4pt" padding="3pt" >
                    <fo:block>
                        <xsl:value-of select="Title"/>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
    </xsl:stylesheet>

结果是(在两页视图中):

enter image description here