【问题标题】:xsl-fo table creation from xml从 xml 创建 xsl-fo 表
【发布时间】:2014-06-13 03:28:53
【问题描述】:

对于我们要构建的表,我有以下 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>

【问题讨论】:

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


    【解决方案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)

    【讨论】:

    • 谢谢,这正是我所需要的。很抱歉这个模糊的问题,但已经晚了,我想有人会理解这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多