-
Notifications
You must be signed in to change notification settings - Fork 10
Under the hood
Knowing all the procedures, syntax and process of how-to is nice. But that's nothing when you want to actually implement this library into your system. So how does this work?
Suppose you have following files:
- static.xsl with your static assets (see second chapter)
- template.xsl with your actual transformation - the one that calls
generate_excel
template - data.xml with your data (eg. data from database, in our case)
- picture.jpg with the picture, you want to have added into final excel
Then, there's couple of options. First, the easiest is to extract all files into same folder and run transform
saxon parser on the template.xsl
.
When you run this (see one of the examples in showcase directory) it will produce valid internal structure of an excel file. With one exception - the binary file picture.jpg
will be just blank file.
So, after finishing the transformation, we loop through binary files we have (in this case that would be picture.jpg only) and copy them into respective places where we find the file with the same name.
Afterwards, we just zip it.
So wrapping up the implementation into bullet points, we work as follows:
- Extract all .xsl related content into
tmp
directory - Put the
data.xml
intotmp
directory - Put all binary assets that might be used into
tmp/binaries
directory -
$ cd tmp/output && transform -xsl:../template.xslt -s:../data.xml
(modify the command to your needs) - Loop everything we have in
tmp/binaries
and search for those files withintmp/output
. If any of the files is found, replace theoutput
file with the one frombinaries
directory cd tmp/output && zip -m -r report.zip * && mv report.zip report.xslx
And that's essentially it!
If you're Java programmer, you can build your own UriResolver
for Saxon. In the URI resolver, you could look for the binary file and have saxon handle it by default.
Note that, I cannot confirm nor deny this, as I was not responsible for saxon implementation. All I am is an XSLT user - therefore, all I do is to have
result-document
touch the binary file and our parsing engine takes care for the rest. I have no idea how, though.
There might be more to this. Feel free to report any discrepancies and/or any suggestions to this section or the guide using issues on github , or just send me an e-mail