- How To Convert Xml To Pdf
- Converting Xml To Html
- Xml To Csv Using Xslt
- Convert Xml To Xsl
- How To Convert Xml To Excel
How To Convert Xml To Pdf
Here are few more advantages of using XSLT. Server-side XSLT is that the application is free of browser compatibility issues. XSLT is used for user defined transformations to XML document and the output can be HTML, XML, or text. So it is easy to merge XML data into presentation.
Easily and rapidly convert XML data to HTML and PDF
Storing data, for example as part of an XForms/REST/XQuery [XRX] architecture (see Related topics), is commonplace today. You can query, retrieve, and serialize data stored in this manner to the desired format. For web applications, developers might want to allow users to retrieve data in HTML format (to be viewed in their web browser) or as a PDF file (to be downloaded for later use).
This article shows how you can convert XML data to HTML, to XSL-FO from that HTML, and to PDF from the XSL-FO with the help of an XQuery extension function based on the CSSToXSLFO tool (see Related topics).
Converting XML data to other formats
Typically, you need two XSL stylesheets to transform data from XML to HTML and XSL-FO: one to transform XML to HTML and one XSL-FO stylesheet to transform the HTML to an XSL-FO document, which you then convert to PDF using an XSL-FO processor. This process means that in an environment such as a web application that generates reports in HTML and PDF, you must write a new XSLT stylesheet that transforms data to HTML, and then write, debug, and maintain the corresponding XSL-FO stylesheet. Keeping up with these stylesheets can be difficult, and XSL-FO isn't easy to work with. Fortunately, because the reports don't have to be sophisticated, you can easily express the layout in a CSS file. Then, you can further process the CSS file using the
CSSToXSLFO
utility, which can generate the XSL-FO document with just a bit of coding. This process and other, similar situations are perfect use cases for the approach that this article presents—namely, writing an XSLT stylesheet that transforms XML to HTML, then adding a few extra CSS instructions to it so that it can transform the HTML to XSL-FO and then to PDF.
The CSSToXSLFO utility
The
CSSToXSLFO
utility allows conversion of an XML document, together with a CSS version 2 (CSS2) stylesheet, into an XSL-FO document. To use this utility, I developed an XQuery extension function to the eXist XML database as part of the XQuery extension module for digital publishing. This utility processes most of the CSS2 specifications. For dealing with specific XSL-FO features, it provides several CSS extension instructions that browsers typically ignore. Use these properties in the
@page
rule of the @media
print section in the CSS stylesheet. The properties are related to page regions, numbering, references, leaders, named strings, hyphenation, footnotes, external graphics, and foreign elements. Be sure to look at the manual for the CSSToXSLFO
utility (see Related topics) for more information on the tool and tips for refining the XSLT stylesheets that you design. The eXist XML database
eXist-db is an open source database management system built completely with XML technology. It supports, among other standards, XQuery, XPath, and XSLT. eXist stores data according to the XML data model and is highly compliant with the XQuery standard. The stored data is processed with XQuery in an index-based manner. The database also has a full-text index based on Apache Lucene.
The XQuery engine of eXist is extensible, so eXist has various XQuery extension modules. These modules provide XQuery extension functions, such as those for:
- Global key-value cache
- Various compression operations
- Additional operations on date and time types
- Various operations on files and directories
- HTTP requests (an XPath module)
- Operations on images stored in the database, including retrieving image dimensions, creating thumbnails, and resizing images
- Accessing and manipulating Java™ Naming and Directory Interface-based directories, such as LDAP
- Sending text or HTML emails
- Scheduling jobs and manipulating existing jobs
- Performing SQL operations against the relational database management system
- Determining the differences between XML nodes
- XSL-FO rendering
- XProc functionality
- Cryptographic operations
Both eXist and
CSSToXSLFO
are written in the Java language. At the time of writing, eXist allows you to use the Apache Formatting Objects Processor (FOP) or RenderHouse XEP as the XSL-FO processor. Check the eXist website for instructions on installing the database so you can run the examples for this article. The eXist module for digital publishing
Vob2mpg v3 0 pro. The eXist module for digital publishing is currently under development and will comprise more functions in the future. For now, it has the
html-to-xslfo()
function, which helps with the approach provided in this article. The intention behind this module is to provide a single source for all the XQuery extension functions needed for digital publishing, including conversion between various formats, such as DocBook, Open XML, DOC, DOCX, .html, PDF, TXT, RTF, PPT, PPTX, and CSV. To install this module within eXist:
- Download the eXist digital publishing module JAR, and copy it into $EXIST_HOME/lib/extensions.
- Download css2xslfo1_6_2.jar, and copy it into $EXIST_HOME/lib/user.
- Add
<module uri='http://kuberam.ro/k-Pub'/>
to the built-in modules section in the $EXIST_HOME/conf.xml file.
Examples
This section analyzes the use of
CSSToXSLFO
as it is implemented in the XQuery extension module of eXist for digital publishing. For this task, use the XML data presented in Listing 1, along with the XSLT stylesheet (xml-to-html.xsl) included in the code example available for download. Note: To make this article easier to read, I didn't include the complete contents of the XSLT stylesheet used. Instead, I present only those elements that make the stylesheet suitable for converting XML to HTML and PDF.
The examples will be uploaded to an eXist XML database in a collection called html-and-pdf-single-stylesheet located in the root collection of eXist, so you can view each example in your browser. For example, to see example 1, type this URL in your browser's address bar (assuming that eXist is installed locally):
To use much more of the functionality in the
CSSToXSLFO
tool, you don't need any other XQuery code beyond that provided in Listing 2 and Listing 3, which render XML data in HTML format and PDF, respectively. To obtain increasingly refined PDF documents, you must add CSS instructions to the CSS section of the XSLT stylesheet. Listing 1. An XML document representing a summary of issued invoices (file xml-data.xml in the example code)
Neela vaana odayil tamil mp3 song free download. The first example, in Listing 2, is an XQuery script that transforms the XML data presented in Listing 1 to HTML. The script uses the
transform:transform()
function of eXist, which in turn transforms XML data using an XSLT stylesheet and (optionally) parameters for transformation. You can write the XSLT stylesheet in XSLT 1.0 (based on Apache Xalan) or XSLT 2.0 (optional with Saxon). Listing 2. XQuery script that transforms XML data to HTML (file example-01.xql in the example code)
Figure 1 shows the rendering of the resulting HTML document in my browser. The HTML document displays a summary of details for three invoices in a non-serif font. (View a text version of the formatted content found in Figures 1 and 2.)
Figure 1. The result of the transformation to HTML
First, the XML data is converted to HTML, as in the previous example. The resulting HTML document contains all the CSS instructions needed for rendering the HTML document as intended, along with the CSS extension instructions specific to
CSSToXSLFO
that will help in using more sophisticated features of XSL-FO. For a simple use case such as the one presented in this article, you do not need such extension instructions. The
CSSToXSLFO
utility transforms the HTML document into an XSL-FO document that in turn generates a PDF document that will closely resemble the HTML document. Next, you convert the resulting HTML document to an XSL-FO document using the
html-to-xslfo()
function, then generate a PDF document as in Listing 3. To create the PDF, use the render()
function of the xslfo
eXist module. Listing 3. XQuery script that transforms XML data to PDF format (file example-02.xql in the example code)
Figure 2 shows the rendered resulting PDF document in my browser. The PDF document displays a summary of details for three invoices in a serif font. (View a text version of the formatted content found in Figures 1 and 2.)
Figure 2. The result of the transformation to PDF format
Listing 4 contains the CSS instructions needed to render XML data in both HTML and PDF. To obtain a similar PDF file, I added only one CSS instruction—to make the table headers bold.
Listing 4. CSS instructions to render XML to both HTML and PDF with a similar appearance
Conclusion
Converting Xml To Html
In this article, you used a simple function to convert XML data to both HTML and PDF formats using just the power and simplicity of CSS syntax and some extension instructions to deal with more complex XSL-FO features. This approach is particularly useful in situations where you have reports or documents with simple styling.
Downloadable resources
- Complete code examples (html-and-pdf-single-stylesheet.zip | 4KB)
Xml To Csv Using Xslt
Related topics
- Web Development with XRX: Find more information about the XRX environment.
CSSToXSLFO
User Guide: Check out more stylesheet and formatting ideas.- eXist XML database website: Download eXist and get instructions on how to install it.
- The
eXist module for digital publishing
: Download from Google Code. - The
CSSToXSLFO
utility: Download this utility from SourceForge. - XML area on developerWorks: Find the resources you need to advance your skills in the XML arena, including DTDs, schemas, and XSLT. See the XML technical library for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks.
- IBM certification: Find out how you can become an IBM-Certified Developer.
- developerWorks on Twitter: Join today to follow developerWorks tweets.
- IBM product evaluation versions: Get your hands on application development tools and middleware products.
Convert Xml To Xsl
Comments
How To Convert Xml To Excel
Sign in or register to add and subscribe to comments.