AxKit.org [logo curtesy of http://xml.com]
--sep--
Start Navigation
About AxKit
Index
xml.apache.org
Features
Live Sites
Installation
Documentation
Daily Churn
Getting AxKit
License
Download
Mailing List
Contribute
CVS
Support
Bugs
End Navigation
Prev Top Next

Sample Document Transformations

Now, we're going to see how AxKit works by transforming an XML file containing data about Camelids (note the dubious Perl reference) into HTML.

Step 1 - A Sample XML Document

First, you will need a sample XML file. Open the text editor of your choice and type the following:

<?xml version="1.0"?>
  <dromedaries>
    <species name="Camel">
      <humps>1 or 2</humps>
      <disposition>Cranky</disposition>
    </species>
    <species name="Llama">
      <humps>1 (sort of)</humps>
      <disposition>Aloof</disposition>
    </species>
    <species name="Alpaca">
      <humps>(see Llama)</humps>
      <disposition>Friendly</disposition>
    </species>
</dromedaries>
Save this file as test.xml.

Step 2 - Create a Stylesheet

Now, create the stylesheet to transform your XML document. If you have chosen to install Sablotron or one of the other XSLT processors that AxKit supports you may use either the XSLT or XPathScript samples below. If you have not installed an XSLT processor skip directly to the XPathScript example.

Using XSLT

Start a new file and type the following:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
  <html>
  <head><title>Know Your Dromedaries</title></head>
  <body>
    <table bgcolor="eeeeee" border="1">
    <tr>
    <th>Species</th>
    <th>No of Humps</th>
    <th>Disposition</th>
    </tr>
    <xsl:for-each select="dromedaries">
      <xsl:apply-templates select="./species" />
  </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

<xsl:template match="species">
  <tr>
  <td><xsl:value-of select="@name" /></td>
  <td><xsl:value-of select="./humps" /></td>
  <td><xsl:value-of select="./disposition" /></td>
  </tr>
</xsl:template>

</xsl:stylesheet>
Save this file as test.xsl.

Using XPathScript

Create a new file and type:

<%
$t->{'humps'}{'pre'} = "<td>";
$t->{'humps'}{'post'} = "</td>";
$t->{'disposition'}{'pre'} = "<td>";
$t->{'disposition'}{'post'} = "</td>";
$t->{'species'}{testcode} = sub {
    my $node = shift;
    my $t = shift;
    $t->{pre} = '<tr><td>' . findvalue('@name', $node) . '</td>';
    $t->{post} = "</tr>";
    return 1;
}
%>

<html>
<head>
        <title>Know Your Dromedaries</title>
</head>
<body bgcolor="white">
    <table bgcolor="eeeeee" border="1">
    <tr><th>Species</th><th>No. of Humps</th><th>Disposition</th></tr>

    <%= apply_templates('/dromedaries/species') %>

    </table>
</body>
</html>
Save this file as test.xps.

Step 3 - Associate the XML Document with your Stylesheet

Next, re-open the test.xml file and add the following just after the <?xml version="1.0"?> declaration. If you have selected the XSLT example, add:

<?xml-stylesheet href="test.xsl" type="text/xsl"?>
Or, if you have chosen the XPathScript sample, use:
<?xml-stylesheet href="test.xps" type="application/x-xpathscript"?>
Note that this line is telling AxKit which stylesheet to use and which handler to use for that stylesheet. Now, save the test.xml file and quit your editor.

Step 4 - Finishing Up

You are now ready to deliver your little zoological XML data file as formatted HTML! Just copy or move the XML and stylesheet files into the same directory under your httpd DocumentRoot. Then, point your browser to http://your-server.com/path/to/test.xml and you should see your data nicely formatted in an HTML table.

Congratulations, you are now well on the road to adding XML, XSLT, XPathScript and AxKit to your developmental toolbelt. For more information about AxKit's advanced features, please visit the AxKit homepage.

If Things Go Wrong

As I stated in the introduction, this is document is designed to walk someone through the AxKit instalation process, on to serving transformed documents as quickly and simply as possible. Depending upon your setup, installing AxKit may require some special attention to get it to sanely co-exist with some of the other tools that you may be using. If you've had any trouble while following the steps outlined here, please consult the AxKit FAQ, and the AxKit User's mailing list archives.


Prev Top Next

Printer Friendly
Raw XML