Pdf 基于列值的xsl fo:表中跨越的动态行数

Pdf 基于列值的xsl fo:表中跨越的动态行数,pdf,xslt-2.0,xsl-fo,apache-fop,Pdf,Xslt 2.0,Xsl Fo,Apache Fop,嗨,我正在尝试根据中的值对单元格进行分组, 使用它可以使用ApacheFop创建PDF 我的代码是 document.xml <data> <data-body> <table-header> <column-one>Column One</column-one> <column-two>Column Two</column-two>

嗨,我正在尝试根据中的值对单元格进行分组, 使用它可以使用ApacheFop创建PDF

我的代码是

document.xml

    <data>
    <data-body>
        <table-header>
            <column-one>Column One</column-one>
            <column-two>Column Two</column-two>
            <column-three>Column Three</column-three>
            <column-four>Column Four</column-four>
        </table-header>
        <table-data>
            <column-one>One</column-one>
            <column-two>5000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>5000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>1200</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>2000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>2000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
         </data-body>
    </data>
<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:template match="data">
            <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="simple"
                        page-height="8.5in" page-width="11in" margin-top=".5in"
                        margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
                        <fo:region-body margin-top="2cm" margin-bottom="2cm" />
                        <fo:region-before extent="2cm" overflow="hidden" />
                        <fo:region-after extent="1cm" overflow="hidden" />
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="simple"
                    initial-page-number="1">
                    <fo:static-content flow-name="xsl-region-before">
                        <fo:block font-size="13.0pt" font-family="serif"
                            padding-after="2.0pt" space-before="4.0pt" text-align="center"
                            border-bottom-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>PDF Test</xsl:text>
                        </fo:block>
                    </fo:static-content>
                    <fo:static-content flow-name="xsl-region-after">
                        <fo:block font-size="12.0pt" font-family="sans-serif"
                            padding-after="2.0pt" space-before="2.0pt" text-align="center"
                            border-top-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>Page</xsl:text>
                            <fo:page-number />
                        </fo:block>
                    </fo:static-content>
                    <fo:flow flow-name="xsl-region-body">
                        <xsl:apply-templates select="data-body" />
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
        <xsl:template match="data-body">
            <fo:block text-align="center">
                <fo:table table-layout="fixed" width="100%"
                    border-style="dashed">
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-header>
                        <xsl:apply-templates select="table-header" />
                    </fo:table-header>
                    <fo:table-body>
                        <xsl:apply-templates select="table-data" />
                    </fo:table-body>
                </fo:table>
            </fo:block>
        </xsl:template>
        <xsl:template match="table-header">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-two"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
        <xsl:template match="table-data">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-two"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
    </xsl:stylesheet>

第一栏
第二栏
第三栏
第四栏
一
5000
三
四
一
5000
三
四
一
1200
三
四
一
2000
三
四
一
2000
三
四
template.xsl

    <data>
    <data-body>
        <table-header>
            <column-one>Column One</column-one>
            <column-two>Column Two</column-two>
            <column-three>Column Three</column-three>
            <column-four>Column Four</column-four>
        </table-header>
        <table-data>
            <column-one>One</column-one>
            <column-two>5000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>5000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>1200</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>2000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
        <table-data>
            <column-one>One</column-one>
            <column-two>2000</column-two>
            <column-three>Three</column-three>
            <column-four>Four</column-four>
        </table-data>
         </data-body>
    </data>
<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <xsl:template match="data">
            <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
                <fo:layout-master-set>
                    <fo:simple-page-master master-name="simple"
                        page-height="8.5in" page-width="11in" margin-top=".5in"
                        margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
                        <fo:region-body margin-top="2cm" margin-bottom="2cm" />
                        <fo:region-before extent="2cm" overflow="hidden" />
                        <fo:region-after extent="1cm" overflow="hidden" />
                    </fo:simple-page-master>
                </fo:layout-master-set>
                <fo:page-sequence master-reference="simple"
                    initial-page-number="1">
                    <fo:static-content flow-name="xsl-region-before">
                        <fo:block font-size="13.0pt" font-family="serif"
                            padding-after="2.0pt" space-before="4.0pt" text-align="center"
                            border-bottom-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>PDF Test</xsl:text>
                        </fo:block>
                    </fo:static-content>
                    <fo:static-content flow-name="xsl-region-after">
                        <fo:block font-size="12.0pt" font-family="sans-serif"
                            padding-after="2.0pt" space-before="2.0pt" text-align="center"
                            border-top-style="solid" border-bottom-width="1.0pt">
                            <xsl:text>Page</xsl:text>
                            <fo:page-number />
                        </fo:block>
                    </fo:static-content>
                    <fo:flow flow-name="xsl-region-body">
                        <xsl:apply-templates select="data-body" />
                    </fo:flow>
                </fo:page-sequence>
            </fo:root>
        </xsl:template>
        <xsl:template match="data-body">
            <fo:block text-align="center">
                <fo:table table-layout="fixed" width="100%"
                    border-style="dashed">
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-column border-style="solid" />
                    <fo:table-header>
                        <xsl:apply-templates select="table-header" />
                    </fo:table-header>
                    <fo:table-body>
                        <xsl:apply-templates select="table-data" />
                    </fo:table-body>
                </fo:table>
            </fo:block>
        </xsl:template>
        <xsl:template match="table-header">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-two"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
        <xsl:template match="table-data">
            <fo:table-row keep-together.within-page="always"
                border-style="solid">
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-one"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-two"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block font-size="10pt" font-family="sans-serif"
                        padding-top="3pt">
                        <xsl:value-of select="column-three"></xsl:value-of>
                    </fo:block>
                </fo:table-cell>
            </fo:table-row>
        </xsl:template>
    </xsl:stylesheet>

PDF测试
页
GeneratePDF.java

package com.test.pdf;
导入java.io.File;
导入java.io.OutputStream;
导入javax.xml.transform.Result;
导入javax.xml.transform.Source;
导入javax.xml.transform.Transformer;
导入javax.xml.transform.TransformerFactory;
导入javax.xml.transform.sax.SAXResult;
导入javax.xml.transform.stream.StreamSource;
导入org.apache.fop.apps.FOUserAgent;
导入org.apache.fop.apps.fop;
导入org.apache.fop.apps.FopFactory;
导入org.apache.fop.apps.MimeConstants;
公共类生成器{
公共静态void main(字符串[]args){
试一试{
System.out.println(“FOP示例XML2PDF\n”);
System.out.println(“准备…”);
//设置输入和输出文件
File xmlfile=新文件(“src/”,“document.xml”);
File xsltfile=新文件(“src/”,“template.xsl”);
File pdffile=新文件(“C:\\Users\\Antony\\Downloads\\pdf”,“ResultPDF.pdf”);
System.out.println(“输入:XML(+xmlfile+”);
System.out.println(“样式表:+xsltfile”);
System.out.println(“输出:PDF(“+pdffile+”));
System.out.println();
System.out.println(“转换…”);
//根据需要配置fopFactory
FopFactory FopFactory=FopFactory.newInstance(新文件(“src/fop.xconf”);
//新文件(“src/fop.xconf”);
FOUserAgent FOUserAgent=fopFactory.newFOUserAgent();
//根据需要配置FousAgent
//设置输出
OutputStream out=新java.io.FileOutputStream(pdffile);
out=新java.io.BufferedOutputStream(out);
试一试{
//使用所需的输出格式构造fop
Fop Fop=fopFactory.newFop(MimeConstants.MIME_PDF,
Fourseragent,out);
//设置XSLT
TransformerFactory=new net.sf.saxon.TransformerFactoryImpl();
变压器=工厂
.新变压器(新StreamSource(xsltfile));
//在样式表中设置a的值
//设置XSLT转换的输入
Source src=新的StreamSource(xmlfile);
//产生的SAX事件(生成的FO)必须通过管道传输
//伪造
Result res=新的SAXSult(fop.getDefaultHandler());
//启动XSLT转换和FOP处理
变换(src,res);
}最后{
out.close();
}
System.out.println(“成功!”);
}捕获(例外e){
System.out.print(“错误!!”);
e、 printStackTrace(System.err);
系统退出(-1);
}
}
}
对于一个特定的列,如果5000在同一列中重复,我需要按顺序合并重复的值 我需要根据值在单个单元格中动态合并5000

我还尝试了**xsl:for each group**group nexting=“column two”column-grouping-key() 但它不像我预期的那样,
请帮助,提前谢谢。

我想您需要使用
组相邻的
,然后您需要传递