Hudzilla.org - the homepage of Paul Hudson
Contents > XML & XSLT > Transforming XML using XSLT Wish List | Report Bug | About Me ]

12.4.3     Making XSL work for its money

This is NOT the latest copy of this book; click here for the latest version.

Now that we can control how our output looks, based upon the XSL stylesheet its processed with, let's try something a little harder that will better demonstrate the flexibility of the XML/XSLT combination.

As mentioned previously, XSLT can transform your output into pretty much any format you want, including unformatted languages like SQL. If the xslt_test.php script is modified to check whether a certain HTTP GET variable is set, we can alter the format of the output merely by changing the URL.

This change needs to be implemented in two steps. Firstly, you need a new XSL stylesheet to transform your content into SQL. Naturally this will look a lot like the previous stylesheet, because the logic is basically the same: loop through all /channel/item elements, and output data about it.

Here is the XSL stylesheet necessary to transfer our example XML into SQL. Save this as sql.xsl in the same directory as the previous files.

<?xmlversion="1.0"encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://my.netscape.com/rdf/simple/0.9/"
    >

    <xsl:output method="html" indent="no" encoding="utf-8" />

    <xsl:template match="/">
        <xsl:for-each select="/channel/item">
            INSERT INTO News (Title, Link) VALUES ('<xsl:value-of select="title"/>', '<xsl:value-of select="url"/>')<BR/>
        </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

The key differences are that we no longer output any HTML. Our output target is different now, and HTML would not work in a MySQL query. Also, this time the stylesheet prints out the "url" value of the XML, along with the "title" value, nested inside an SQL query.

In order to facilitate the output selection, you also need to modify the xslt_test.php script so that it changes the input XSL files based upon the value of a variable. There are various ways to do this, but below I have included an example to get you started:

<?php
    $xsltproc
= xslt_create();

    if (isset(
$USESQL)) {
        
$xslinput = 'sql.xsl';
    } else {
        
$xslinput='formatted.xsl';
    }

    
$hResult=xslt_process($xsltproc,'final.rss',$xslinput);
    print
$hResult;
    
xslt_free($xsltproc)
?>




<< 12.4.2 Handling the processed output   12.4.4 What else can XSL do? >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is three plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow