【发布时间】:2018-08-18 13:58:21
【问题描述】:
我需要将 XML 文件转换为 PDF,通过 XSL-FO 来完成。 源 XML 文件具有其结构和字典 (NITF),不应更改。我必须为这些文件创建特定的 XSL 样式器。在整个 XML 元素中,我只需要几个:
文字
-
表格
图像
到目前为止,我已经成功地转换了 XML 文件的文本部分。而且我可以处理只包含一个具有固定列号的简单表的文件。当我尝试同时处理源文件中的文本和表格时,出现转换错误。 附加了(坏工作)样式器 my.xsl 以及源文件。这些错误有点
org.apache.fop.fo.ValidationException:“fo:table-body”缺少子元素。所需内容模型:marker* (table-row+|table-cell+)
XML:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE nitf SYSTEM "nitf.dtd"> <nitf> <head> <title type="main">Sub-title 1</title> <meta name="filetype" content="content"/> <docdata><document-id id-string="123456" /></docdata> </head> <body> <body.head> <hedline><hl1>Sub-title 1</hl1></hedline> </body.head> <body.content> <ul> <li>Some long text 1</li><li>Some long text 2</li> </ul> <table id="0001.csv"> <tbody> <tr> <td colspan="4" class="tbh">Table tilte 1</td> </tr> <tr> <td colspan="1" class="tbc"> </td> <td colspan="1" class="tbc-r">Col title 1</td> <td colspan="1" class="tbc-r">Col title 2</td> <td colspan="1" class="tbc-r">Col title 3</td> </tr> <tr> <td colspan="1" class="tbd">Row title 1</td> <td colspan="1" class="tbd-r">cell text 1</td> <td colspan="1" class="tbd-r">cell text 2</td> <td colspan="1" class="tbd-r">cell text 3</td> </tr> <tr> <td colspan="1" class="tbd">Row title 2</td> <td colspan="1" class="tbd-r">cell text 4</td> <td colspan="1" class="tbd-r">cell text 5</td> <td colspan="1" class="tbd-r">cell text 6</td> </tr> <tr> <td colspan="4" class="footnote">Some footnote</td> </tr> <tr> <td colspan="4" class="source">One more footnote</td> </tr> </tbody> </table> <p class="text">Just a short text</p> <ul> <li>Some long text 3</li><li>Some long text 4</li> </ul> </body.content> </body>XSL:
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" indent="yes"/> <xsl:template match="nitf"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master page-height="297mm" page-width="210mm" margin="5mm 25mm 5mm 25mm" master-name="simpleA4"> <fo:region-body margin="20mm 0mm 20mm 0mm"/> </fo:simple-page-master> </fo:layout-master-set> <!-- NOTE: text part is OK! --> <fo:page-sequence master-reference="simpleA4"> <fo:flow flow-name="xsl-region-body" > <fo:block> <xsl:apply-templates select="head"/> <!--xsl:apply-templates select="body"/ If it's uncommented, the table is not seen--> </fo:block> <fo:block> <fo:table table-layout="fixed" border-style="solid"> <xsl:apply-templates select="tr" mode="theader"/> <xsl:apply-templates select="tr" mode="tbody"/> <fo:table-body> <xsl:apply-templates select="body/table/tbody/tr"/> </fo:table-body> </fo:table> </fo:block> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="tr"> <fo:table-row> <xsl:apply-templates select="td"/> </fo:table-row> </xsl:template> <xsl:template match="td"> <fo:table-cell border-style="solid"> <fo:block><xsl:value-of select="."/></fo:block> </fo:table-cell> </xsl:template> <!-- text --> <xsl:template match="head"> <fo:inline font-weight="bold"> <xsl:apply-templates/> </fo:inline> </xsl:template> <xsl:template match="body.head"> <fo:inline font-weight="bold"> <xsl:apply-templates/> </fo:inline> </xsl:template> <xsl:template match="body.content"> <xsl:apply-templates/> </xsl:template> <xsl:template match="p"> <fo:block> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="b"> <fo:inline font-weight="bold"> <xsl:apply-templates/> </fo:inline> </xsl:template> </xsl:stylesheet >【问题讨论】:
-
您的 XSL 有很多问题。没有具有“theader”或“tbody”模式的模板,因此它们什么都不做。您在表体内的匹配项是“body/table/tbody/tr”,它在您的 XML 中也不存在(至少会缺少“body.content”。它还会将许多表合并为一个。真正的问题这就是为什么要尝试编写已经存在的内容。请参阅下面的答案。
-
标签: xml xslt xsl-fo apache-fop