从XML文件开始使用Apache FOP从XSL-FO转换为PDF



我是XSL-FO编程的新手,需要帮助。我基本上需要用XML中的数据将XSL-FO文件渲染成PDF。

但当我尝试时,PDF(背景,手动输入的文本)中的所有内容都显示正确,而不是应该从XML中读取的数据。

请注意,我只尝试使用元素"idShip"。

我的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE shipment SYSTEM "C:UsersinformaticaDocumentsworkspacePrintTestshipment.dtd">
<?xml-stylesheet href="shipment_to_pdf.xsl" ?>
<shipment>
    <idShip>111</idShip>
    <fare></fare>
    <to>
        <name></name>
        <surname></surname>
        <address1></address1>
        <address2></address2>
        <zip></zip>
        <city></city>
        <country></country>
        <email></email>
        <phone></phone>
    </to>
    <from>
        <name></name>
        <surname></surname>
    </from>
</shipment>

XSL:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <fo:layout-master-set>
                <fo:simple-page-master page-height="135mm"
                    page-width="216mm" margin-top="10mm" margin-left="20mm"
                    margin-right="20mm" margin-bottom="10mm" master-name="PageMaster">
                    <fo:region-body background-color="#EFAFAF"
                        margin-top="20mm" margin-left="10mm" margin-right="10mm"
                        margin-bottom="20mm" />
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence initial-page-number="1"
                master-reference="PageMaster">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block text-indent="1em" font-family="sans-serif"
                        font-size="20pt" font-weight="bold" background-color="#EEEEEE"
                        line-height="20mm">
                        //this should be the problem 
                        <xsl:for-each select="shipment">
                            <xsl:value-of select="idShip" />
                        </xsl:for-each>
                     </fo:block>
                </fo:flow>

            </fo:page-sequence>     
</fo:root>

PDF现在呈现为红色背景的块,但没有来自XML的数据。

您使用的是一个所谓的简化XSLT模块(XSLT1.1推荐,XSLT2.0推荐),它几乎是正确的,只是在根元素中缺少xsl:version属性。

只需将xsl:version="1.0"(或者2.0,如果您需要您的XSLT处理器支持这些新特性)添加到fo:root元素中,就会得到预期的结果。

请注意,简化的样式表有一些限制,因为您不能定义参数、全局变量、函数(如果使用XSLT2.0)、键和其他模板。

最新更新