Introduction to Server-Side XML

It sets the scene for server-side XML, and shows what you can do with it, by way of a parallel example done in ASP, PHP, and JSP (we have only included the first of the example sections here). The three chapters that follow this one in the book are case studies, which go into using XML with the three server-side languages mentioned above in much more detail.

This sample is taken from Chapter 8 "Introduction to Server-Side XML" of the glasshaus title "Practical XML for the Web".

Transforming the XML

Our first task is to display the list of CDs in HTML format. We need to create an XSLT stylesheet to transform the XML to HTML, and then we'll create a server-side page to carry out the transformation (that is, to apply the XSLT to the XML, and output the resulting HTML).

We just want a plain HTML table, listing the CDs ordered first by band and then by name. We also need links for modifying and deleting, and a link for adding a new CD. We want the page to look something like the following:


The following very simple XSLT stylesheet (cd_list.xsl) accomplishes this:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" omit-xml-declaration="yes" />

<xsl:template match="/cd-list">

  <html>

    <head>

      <title>CD List</title>

      <style>

        table {

          color: #000000;

          font-family:  Arial, Helvetica, sans-serif;

          font-size: 11px;

        }

        th {

          font-size: 12px;   

          font-weight: bold;

        }

        a {

          color: #000000;

          font-weight: bold;

        }

        a:hover {

          text-decoration: underline;

        }

      </style>

    </head>

    <body>

      <table border="1" cellpadding="3" width="300">

        <tr>

          <th>Name</th>

          <th>Band</th>

          <th>Year</th>

          <th>Delete</th>

        </tr>

        <xsl:apply-templates select="cd">

          <xsl:sort select="band"/>

          <xsl:sort select="name"/>

        </xsl:apply-templates>

        <tr>

          <td colspan="4"><a href="cd_edit.(asp/php/jsp)">Add New</a></td>

        </tr>

      </table> 

    </body>

  </html>

</xsl:template>

<xsl:template match="cd">

  <tr>

    <td>


      <a>

        <xsl:attribute name="href">

          cd_edit.(asp/php/jsp)?id=<xsl:value-of select="@id"/>

        </xsl:attribute>

        <xsl:value-of select="name"/> 

      </a>

    </td>

    <td>

      <xsl:value-of select="band"/>

    </td>

    <td>

      <xsl:value-of select="year"/>

    </td>

    <td>

      <a>

        <xsl:attribute name="href">

          cd_delete.(asp/php/jsp)?id=<xsl:value-of select="@id"/>

        </xsl:attribute>

        delete

      </a>

    </td>

  </tr>

</xsl:template>

</xsl:stylesheet>

There is not much to explain here. We simply create a <table> that contains a row for each CD. The name of the CD is a link to cd_edit.(asp/php/jsp), and the id of the CD is sent as a query string variable:

<a>

    <xsl:attribute name="href">

      cd_edit.(asp/php/jsp)?id=<xsl:value-of select="@id"/>

    </xsl:attribute>

    <xsl:value-of select="name"/> 

</a>

The cd_edit.(asp/php/jsp) page is a page that contains a form to allow the user to modify the CD information. This page will be created later. In the same way, the last column of each row contains a link to cd_delete.(asp/php/jsp) for deleting the CD from the list. The id is sent as a query string variable. Finally, at the bottom of the table there is a link to cd_edit.(asp/php/jsp) for adding a new CD (note that no id is sent, since a new CD does not yet have an id).

NOTE: We'll use this stylesheet almost unchanged for ASP, PHP, and JSP. The only thing that changes is the file extension in the links (.asp, .php, or .jsp). In the above section I've therefore included (asp/php/jsp) as the extension, to emphasize that the file extension depends on the server-side technology being used.

The next thing is to create the page that performs the transformation on the server side.


George Petrov

George PetrovGeorge Petrov is a renowned software writer and developer whose extensive skills brought numerous extensions, articles and knowledge to the DMXzone- the online community for professional Adobe Dreamweaver users. The most popular for its over high-quality Dreamweaver extensions and templates.

George is also the founder of Wappler.io - the most Advanced Web & App Builder

See All Postings From George Petrov >>

Comments

Be the first to write a comment

You must me logged in to write a comment.