简体   繁体   English

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

[英]Conversion from XSL-FO to PDF with Apache FOP starting from a XML file

I'm new to XSL-FO programming and I need help.我是 XSL-FO 编程的新手,我需要帮助。 I basically need to render a XSL-FO file into PDF with data from XML.我基本上需要使用 XML 中的数据将 XSL-FO 文件渲染为 PDF。

But when I try, everything appears right int the PDF (background, manually entered text) but not data that should be read from XML.但是当我尝试时,一切都出现在 PDF(背景,手动输入的文本)中,但不是应该从 XML 读取的数据。

Notice that I tried only with the element "idShip".请注意,我仅尝试使用元素“idShip”。

My XML file:我的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE shipment SYSTEM "C:\Users\informatica\Documents\workspace\PrintTest\shipment.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: 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 right now renders as a block with a red background, but no data from XML. PDF 现在呈现为具有红色背景的块,但没有来自 XML 的数据。

You are using a so-called simplified XSLT module ( XSLT 1.1 recommendation , XSLT 2.0 recommendation ) which is almost correct, it is only missing the xsl:version attribute in the root element.您正在使用所谓的简化 XSLT 模块XSLT 1.1 推荐XSLT 2.0 推荐),这几乎是正确的,它只是在根元素中缺少xsl:version属性。

Just add xsl:version="1.0" (or 2.0 if you need the new features and your XSLT processor supports them) to the fo:root element and you should get the expected result.只需将xsl:version="1.0" (或2.0如果您需要新功能并且您的 XSLT 处理器支持它们)添加到fo:root元素,您就会得到预期的结果。

Note that a simplified stylesheet has some limitations, as you cannot define parameters, global variables, functions (if using XSLT 2.0), keys and other templates.请注意,简化的样式表有一些限制,因为您不能定义参数、全局变量、函数(如果使用 XSLT 2.0)、键和其他模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用XSLT和XSL-FO / FOP将XML文件转换为PDF - Convert an XML file to PDF using XSLT and XSL-FO/FOP 如何使用XSL-FO样式表和Apache FOP将XML文件中的不同表转换为PDF - how to transform different tables in a XML file to PDF by using a XSL-FO stylesheet and Apache FOP apache fop:从 xml 和 xsl 格式文件生成 pdf 时出现问题 - apache fop: Problem generating a pdf from a xml and xsl formatting file 如何使用 XSL-FO(和 Apache FOP)计算从应用模板返回的元素 - How to count elements returned from applied template using XSL-FO (and Apache FOP) 将从 Word 粘贴的文本转换为 Firefox 富文本编辑器到 xsl-fo,同时保留格式 (Java) (Apache FOP) - Convert text pasted from Word into a Firefox rich-text editor to xsl-fo while preserving formatting (Java) (Apache FOP) XSLT - XML(Word)到XSL-FO(PDF) - XSLT - XML (Word) to XSL-FO (PDF) 在使用FOP的XSL:FO从XML生成pdf的同时,如何在所有页面中添加动态标头? - How to add dynamic header in all the pages while generating pdf from XML using XSL:FO using fop? 如何使用 MathML 从 XSL-FO 转换为 PDF - How to convert to PDF from XSL-FO with MathML xsl-fo,org.apache.fop.fo.ValidationException:“ fo:table-body”缺少子元素 - xsl-fo, org.apache.fop.fo.ValidationException: “fo:table-body” is missing child elements 使用xsl-fo将链接放入PDF文件 - Put a link in a PDF file with xsl-fo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM