我的xsl-fo代码出了什么问题

时间:2012-12-03 22:44:50

标签: apache xslt xsl-fo apache-fop

我正在尝试使用以下命令中的apache-fop生成pdf。

C:\fop-1.0-bin\fop-1.0>fop -xml sample1.xml -xsl xsl-fo.xsl -pdf sample2.pdf

我的xml看起来像这样

<?xml version="1.0" encoding="iso-8859-1"?>
<service>
    <serviceId>11111</serviceId>
    <openDate>2012-02-16</openDate>
    <closeDate>2012-02-16</closeDate>
    <description>INSPECT MACHINE</description>
    <repairCost>$266.76</repairCost>
</service>

我的xsl-fo xslt文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    >
    <xsl:output encoding="iso-8859-1" />
    <xsl:template match ="Service">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="Service">
                    <fo:region-body></fo:region-body>
                </fo:simple-page-master>
            </fo:layout-master-set>

            <fo:page-sequence master-reference="Service">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block>
                        <fo:table>
                            <fo:table-body>
                                <fo:table-row>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Service Id</fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Open Date</fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Close Date</fo:block>
                                    </fo:table-cell>                                    
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Order Status</fo:block>
                                    </fo:table-cell>                                    
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Description</fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px black" 
                                    text-align="center" font-weight="bold">
                                        <fo:block>Repair Cost</fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <xsl:for-each select="./servicetype">
                                    <fo:table-row>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="serviceId" /></fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="openDate" /></fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="closeDate" /></fo:block>
                                        </fo:table-cell>                                

                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="description" /></fo:block>
                                        </fo:table-cell>
                                        <fo:table-cell border="solid 1px bold" text-align="center">
                                            <fo:block><xsl:value-of select="repairCost" /></fo:block>
                                        </fo:table-cell>
                                    </fo:table-row>
                                </xsl:for-each>
                            </fo:table-body>
                        </fo:table>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>

我收到错误。有人可以在代码中指出我的错误。我正在关注此示例http://www.codeproject.com/Articles/37663/PDF-Generation-using-XSLFO-and-FOP

SEVERE: org.apache.fop.fo.ValidationException: Document is empty (something migh
t be wrong with your XSLT stylesheet).
Dec 3, 2012 4:27:27 PM org.apache.fop.cli.Main startFOP
SEVERE: Exception
javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException:
 Document is empty (something might be wrong with your XSLT stylesheet).
        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:302)
        at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130)
        at org.apache.fop.cli.Main.startFOP(Main.java:174)
        at org.apache.fop.cli.Main.main(Main.java:205)
Caused by: javax.xml.transform.TransformerException: org.apache.fop.fo.Validatio
nException: Document is empty (something might be wrong with your XSLT styleshee
t).
        at org.apache.xalan.transformer.TransformerImpl.transformNode(Transforme
rImpl.java:1430)
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp
l.java:709)
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp
l.java:1284)
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp
l.java:1262)
        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299)
        ... 3 more
Caused by: org.apache.fop.fo.ValidationException: Document is empty (something m
ight be wrong with your XSLT stylesheet).
        at org.apache.fop.events.ValidationExceptionFactory.createException(Vali
dationExceptionFactory.java:38)
        at org.apache.fop.events.EventExceptionManager.throwException(EventExcep
tionManager.java:54)
        at org.apache.fop.events.DefaultEventBroadcaster$1.invoke(DefaultEventBr
oadcaster.java:175)
        at $Proxy0.emptyDocument(Unknown Source)
        at org.apache.fop.fo.FOTreeBuilder.endDocument(FOTreeBuilder.java:158)
        at org.apache.xml.serializer.ToXMLSAXHandler.endDocument(ToXMLSAXHandler
.java:181)
        at org.apache.xalan.transformer.TransformerImpl.transformNode(Transforme
rImpl.java:1378)
        ... 7 more

3 个答案:

答案 0 :(得分:2)

尝试更改foreach中的xpath

<xsl:for-each select="./service">

的xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<services>
    <service>
        <serviceId>11111</serviceId>
        <openDate>2012-02-16</openDate>
        <closeDate>2012-02-16</closeDate>
        <orderStatus>Status2</orderStatus>
        <description>INSPECT MACHINE</description>
        <repairCost>$266.76</repairCost>
    </service>
    <service>
        <serviceId>11111</serviceId>
        <openDate>2012-02-16</openDate>
        <closeDate>2012-02-16</closeDate>
        <orderStatus>Status1</orderStatus>
        <description>INSPECT MACHINE</description>
        <repairCost>$266.76</repairCost>
    </service>
</services>

的xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output encoding="iso-8859-1" />
<xsl:template match ="services">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="service">
                <fo:region-body></fo:region-body>
            </fo:simple-page-master>
        </fo:layout-master-set>

        <fo:page-sequence master-reference="service">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    <fo:table>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Service Id</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Open Date</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Close Date</fo:block>
                                </fo:table-cell>                                    
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Order Status</fo:block>
                                </fo:table-cell>                                    
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Description</fo:block>
                                </fo:table-cell>
                                <fo:table-cell border="solid 1px black" 
                                text-align="center" font-weight="bold">
                                    <fo:block>Repair Cost</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <xsl:for-each select="./service">
                                <fo:table-row>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="serviceId" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="openDate" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="closeDate" /></fo:block>
                                    </fo:table-cell>                                
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="orderStatus" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="description" /></fo:block>
                                    </fo:table-cell>
                                    <fo:table-cell border="solid 1px bold" text-align="center">
                                        <fo:block><xsl:value-of select="repairCost" /></fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </xsl:for-each>
                        </fo:table-body>
                    </fo:table>
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
</xsl:stylesheet>

答案 1 :(得分:2)

它可以像案件的问题一样简单吗?您的示例XML使用<service>作为根标记,但您有<xsl:template match="Service">(大写字母S)。

答案 2 :(得分:0)

FOP不支持自动表格布局,而是使用固定的表格布局

FOP不支持自动布局,因为页面具有静态宽度[A4,A3 ...] 仅在html rendring中支持自动模式,因为页面大小可能会更改(取决于分辨率,调整窗口大小...) 见:

http://mail-archives.apache.org/mod_mbox/xmlgraphics-fop-dev/200510.mbox/%3C20051005124250.8A19A21E@ajax.apache.org%3E

http://wiki.apache.org/xmlgraphics-fop/Troubleshooting/CommonLogMessage

注意:为避免出现警告,您应指定table-layout =“fixed”,并在定义列widht时使用width =“100%”和“*”