Generating an HTML page using XML and XSL transformations

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.

Getting ready

You must have a database of books with the structure shown in the Appendix, Data Structures.

How to do it...

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.

  1. Create a new transformation.
  2. Drop a Table Input step into the canvas and select the books information. Use the following SQL statement:
    SELECT *
    FROM Books
    LEFT JOIN Authors
    ON Books.id_author = Authors.id_author
    
  3. Add an XML Output step from Output category.
  4. Fill in the File tab giving the file the name books and leaving the default xml as the proposed extension.
  5. Under the Content tab, type Books in Parent XML element and Book in Row XML element.
  6. Under the Fields tab, press the Get Fields button, in order to retrieve the entire fields information. Modify the price field giving it the Format

    The 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>
    
  7. Now, you must create the XSL file (booksFormat.xsl), based on the 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>
    

    Note

    You can save a lot of time by downloading the sample XSL file from the book's website!

  8. Create a new job and add a Start entry.
  9. Add a Transformation entry to execute the preceding transformation.
  10. Add an XSL Transformation job entry from the XML category. Set the Files frame to the following:
    How to do it...
  11. Run the job. A file named Book.htm will be created having the following layout:
How to do it...

How it works...

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:

  • One section for the header: a<table> tag with a row containing the fields' headers
  • The tag<xsl:apply-templates select="Book"> indicating a loop over the template Book for each book
  • The template Book, that creates a new row with the field's values

In 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.

There's more...

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:

http://www.w3schools.com/xsl/

See also

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset