Using XSL-FO and HTML?

2020-04-08 11:35发布

问题:

I'm trying to transform some XML-data to HTML with XSLT for my bachelor thesis.

My professor wants me to consider XSL-FO too, or at least to write some word about it. But I'm very noob to this.

So my questions are:

Can I combine FO with HTML? Can I use FO istead of HTML and CSS? If yes, how will my browser render this? Are there any examples/tutorials on how to transform xml into web pages with FO?

回答1:

XSL-FO is for PDF display only (this is not strictly true, but you can take it as a guideline). So HTML output and FO output are not related. From your XML source, you can use XSLT to generate XHTML or XSL-FO, but not both at the same time.

See for example DocBook. It comes with several XSLT stylesheets ready to use, one is for HTML output and one for PDF (via Apache Fop). If you are satisfied with the result could be a different question.



回答2:

Generating XSL-FO and XHTML from XSLT is not necessarily an either-or choice.

XSL-FO is generally used to generate PDF. For that you will need an XSL-FO engine, such as FOP, RenderX, Antenna House, IBex, etc. However, you can convert XSL-FO into XHTML and then render in the browser.

Generally, it wouldn't be worth the hassle to have your XSLT create XSL-FO and then convert into XHTML (just generate XHTML directly), unless you want to create both output formats (PDF and XHTML) with reduced effort.

It is possible to create both **XSL-FO and XHTML at the same time without maintaining two complete sets of stylesheets to create similar output in different vocabularies**.

Rather than choosing between one format or the other, or having to maintain two distinctly diffirent(but similar) stylesheet libraries, you can create your main stylesheet library to generate either XSL-FO or XHTML and then use a second transform to convert from XSL-FO to XHTML and vice-versa. There are existing XSLT stylesheets that you can leverage to do this.

In the past I have developed XSL-FO stylesheets and then used the Render-X FO2HTML stylesheet to convert the XSL-FO to XHTML output. It converts <block> elements into <div>, <inline> into <span>, etc.

I haven't used them before, but you could also try using HTML2FO stylesheets to convert XHTML outut into XSL-FO.

Out of the box, you can get amazingly similar output in both formats while maintaining one XSLT library dedicated to one particular output format.

If you happen to need to customize the output slightly (e.g. different header content for XHTML) then you just need to import/extend the conversion stylesheets and override the appropriate template(s) for the divergent content. This makes maintenance much easier, so you don't have to worry about updating multiple sets of stylesheets with essentially the same information.



标签: xslt xsl-fo