Reputation: 13
I am trying to generate pdf with Apache FOP and I will be receiving input data in JSON format. Is there a way to loop over the JSON input in XSL-FO.
Upvotes: 1
Views: 1323
Reputation: 146
So let's be clear here; you cannot "loop" over anything in XSL-FO, per se. XSL-FO is essentially an output formatting specification language. It is not a "programming language" in the richer senses of that phrase. There is looping that goes on behind the scenes ('for each page, do this and this'), but that's all handled by the engine. You never declare a looping directly.
What you DO do is take in data (presumably XML data, but not always) using XSLT to transform the input data into XSL-FO. This FO is a special kind of XML markup that PDF engines (like FOP, Antenna House Formatter, RenderX, Ecrion, etc.) understand and use to format the actual PDF.
It is the XSLT that does the looping (as explicitly programmed by you) through the original input data.
Now as to that input data being JSON, as Tony Graham stated, XSLT 3.0 makes processing that much easier.
There's a nice little blog post here that you can view which describes the process of going from JSON to XML using XSLT 3.0. Instead of going all the way through, however, and converting the map into a different, neater form of XML, as done in the blog, you can of course just loop through the map elements directly to create your FO.
Now in terms of eliminating the JSON-to-XML step(s), this technically could be done, but would be far more trouble than it's worth just to claim you 'skipped the step'. The efficiency that that phrase would imply would be horribly misleading, as it would not be efficient at all. You would be looking at the XSLT taking as input the JSON as a text file, then doing a boat-load of awkward and unjustified string manipulations to output FO directly from that JSON text, as opposed to making things so much simpler and performing the simple step of utilizing the json-to-xml function and then working from that XML data to produce your FO.
Remember, XSLT is at its best when working with XML data. That's what it was first and foremost designed to do, as opposed to handling large-scale string manipulations like SED or AWK, to take two examples. Though it can take different inputs, like say a CSV file, the optimal way to handle such inputs is to ultimately represent them as XML input, and then let XSLT really shine by building out your FO.
I hope this helps you and anyone else viewing this post as to the relationships between various forms of possible input data, XSLT, XSL-FO, and PDF.
Upvotes: 0
Reputation: 8068
You need to transform your source -- JSON data, in your case -- into XML in the XSL-FO vocabulary that an XSL Formatter understands.
If you want to use XSLT to do the transformation into XSL-FO, you will find this easier to do if you use an XSLT 3.0 processor, since XSLT 3.0 added built-in support for transforming JSON. See, e.g.:
Upvotes: 1