如何编写XSL文件将给定XML内容生成PDF表格?
Hey there! Let's figure out how to convert your XML into a proper PDF table. Directly transforming XML to PDF with plain XSL isn't feasible—instead, we'll use XSLT to convert your XML to XSL-FO (Extensible Stylesheet Language Formatting Objects), then use an FO processor to render that into a PDF. Here's a step-by-step solution:
Step 1: Understand the Conversion Pipeline
The process works like this:
- Your source XML → XSLT stylesheet → XSL-FO document → FO processor (e.g., Apache FOP) → Final PDF
Step 2: Write the XSLT Stylesheet
Your XML has <p> elements in the XHTML namespace, so we need to declare that namespace in our XSLT to properly target those elements. Here's a complete stylesheet that turns your three <item> entries into a single table row (you can adjust this if you need a different table structure):
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output method="xml" indent="yes"/> <!-- Root template --> <xsl:template match="/"> <fo:root> <!-- Define page layout --> <fo:layout-master-set> <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm" margin="20mm"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <!-- Page sequence --> <fo:page-sequence master-reference="A4"> <fo:flow flow-name="xsl-region-body"> <!-- Table definition --> <fo:table border="1pt solid black" table-layout="fixed" width="100%"> <fo:table-body> <fo:table-row> <!-- Process each item into a table cell --> <xsl:apply-templates select="values/item"/> </fo:table-row> </fo:table-body> </fo:table> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <!-- Template for each item: extract the XHTML p text --> <xsl:template match="item"> <fo:table-cell padding="8pt" text-align="center"> <fo:block font-size="12pt"> <xsl:value-of select="html/xhtml:p"/> </fo:block> </fo:table-cell> </xsl:template> </xsl:stylesheet>
Key Notes on This Stylesheet:
- We declare the
xhtmlnamespace to access the<p>elements (since they're inhttp://www.w3.org/1999/xhtml). This is a common gotcha—if you skip this, the XSLT won't find your text content. - The XSL-FO structure defines an A4 page with a bordered table, where each
<item>becomes a centered, padded table cell. - If you need to expand this later (like adding data rows under these headers), you can easily extend the template to handle more complex XML structures.
Step 3: Generate the PDF
To turn the XSL-FO output into a PDF, use an open-source FO processor like Apache FOP:
- Grab Apache FOP from its official distribution (no external links needed—just search for it)
- Save your original XML as
input.xmland the XSLT above astransform.xsl - Run this command in your terminal:
fop -xml input.xml -xsl transform.xsl -output output.pdf
This will spit out a PDF with a clean table containing your three values: Step, Description, Result.
内容的提问来源于stack exchange,提问作者Eronimo

