从xml创建xsl-fo表

时间:2014-06-13 03:28:53

标签: xml pdf xslt html-table xsl-fo

对于我们想要构建的表,我有以下xml格式,对于结构上的循环方式可以正常工作。但是我无法弄清楚xsl-fo转换为PDF。

 <table>
      <thead>
           <tcell>data</tcell>
           <tcell>data</tcell>
           <tcell>data</tcell>
      </thead>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
      <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
 </table>

1 个答案:

答案 0 :(得分:1)

你没有太多问题(只是一些模糊的要求),但这至少应该让你开始。

XML输入

<table>
    <thead>
        <tcell>data</tcell>
        <tcell>data</tcell>
        <tcell>data</tcell>
    </thead>
    <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
    <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
    <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
    <trow><tcell>data</tcell><tcell>data</tcell><tcell>data</tcell></trow>
</table>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                    <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="my-page">
                <fo:flow flow-name="xsl-region-body"> 
                    <xsl:apply-templates/>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>        
    </xsl:template>

    <xsl:template match="table">
        <fo:table>
            <xsl:apply-templates select="thead"/>
            <fo:table-body>
                <xsl:apply-templates select="trow"/>
            </fo:table-body>
        </fo:table>
    </xsl:template>

    <xsl:attribute-set name="trow">
        <xsl:attribute name="border">solid</xsl:attribute>
        <xsl:attribute name="padding-left">1em</xsl:attribute>
        <xsl:attribute name="padding-right">1em</xsl:attribute>
    </xsl:attribute-set>

    <xsl:template match="thead">
        <fo:table-header>
            <fo:table-row background-color="#FFDEAD" text-align="center">
                <xsl:apply-templates/>
            </fo:table-row>
        </fo:table-header>
    </xsl:template>

    <xsl:template match="tcell">
        <fo:table-cell xsl:use-attribute-sets="trow">
            <fo:block><xsl:value-of select="."/></fo:block>
        </fo:table-cell>
    </xsl:template>

    <xsl:template match="trow">
        <fo:table-row>
            <xsl:apply-templates/>
        </fo:table-row>
    </xsl:template>

</xsl:stylesheet>

XSL-FO输出(使用的XSLT处理器:Saxon-HE)

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:table>
            <fo:table-header>
               <fo:table-row background-color="#FFDEAD" text-align="center">
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-header>
            <fo:table-body>
               <fo:table-row>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
               </fo:table-row>
               <fo:table-row>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
                  <fo:table-cell border="solid" padding-left="1em" padding-right="1em">
                     <fo:block>data</fo:block>
                  </fo:table-cell>
               </fo:table-row>
            </fo:table-body>
         </fo:table>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

呈现PDF (使用的XSL-FO处理器:Apache FOP)

enter image description here