Sometimes, you don't have access to the source database from the web server, or you just want static pages in your site. Under this scenario, you can create a web page through XSLT and then publish it. In this recipe, you will take advantage of the XSL Transformation job entry features to do just that: taking an XML file and transforming it into HTML.
Suppose you want to publish a books catalog on a website. In this recipe, you will generate an HTML page taking as its source data that you have in a database.
You must have a database of books with the structure shown in the Appendix, Data Structures.
The first group of steps is meant for exporting the books' information from the database to an XML file, if you already have the information in this format, then you can skip to step 7.
SELECT * FROM Books LEFT JOIN Authors ON Books.id_author = Authors.id_author
books
and leaving the default xml
as the proposed extension. Books
in Parent XML element and Book
in Row XML element. price
field giving it the FormatThe result from these steps will be a file named books.xml with the books structure. It must look like the following:
<Books> <Book> <title>Carrie</title> <price>$41,00</price> <genre>Fiction</genre> <lastname>King</lastname> <firstname>Stephen</firstname> </Book> <Book> <title>Salem›s lot</title> … </Book> … </Books>
books.xml
structure. Create a new file with your preferred text editor and type the following:<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" indent="yes" encoding="UTF-8"/> <xsl:template match="/Books"> <html> <head> <title>Books</title> </head> <body> <h1>Books</h1> <table border="1"> <!-- grid header --> <tr bgcolor="lightblue"><td>Title</td><td>Author</td> <td>Price</td><td>Genre</td></tr> <xsl:apply-templates select="Book"> <xsl:sort select="title" /> </xsl:apply-templates> </table> </body> </html> </xsl:template> <xsl:template match="Book"> <!-- grid value fields --> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="lastname"/>, <xsl:value-of select="firstname"/></td> <td><xsl:value-of select="price"/></td> <td><xsl:value-of select="genre"/></td> </tr> </xsl:template> </xsl:stylesheet>
You can save a lot of time by downloading the sample XSL file from the book's website!
Book.htm
will be created having the following layout:The Extensible Stylesheet Language (XSL) is used to transform and render XML documents. In this recipe, you generated an XML file with books information and then used an XSL file to transform that XML file into an HTML page.
Looking at the XSL file, you can see how it transforms the fields from the source into an HTML code. The file has different sections, which are as follows:
<table>
tag with a row containing the fields' headers<xsl:apply-templates select="Book">
indicating a loop over the template Book
for each book Book
, that creates a new row with the field's valuesIn order to apply the transformation defined in the XSL file effectively, you used the XSL Transformation job entry. The configuration of the entry is straightforward: you simply provide names of the XML file, the XSL file and the resulting file, and you're done.
As an option, right after creating the page, you may publish it automatically on the website. For doing that, simply extend the job with a file transfer entry.
You will find more information about XSL at the following URL:
http://en.wikipedia.org/wiki/XSLT
You can also follow the following tutorial:
The recipe named Putting files on a remote server, in Chapter 4, File Management. See this recipe for instructions on extending the job for transferring the generated page.