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

Basic Techniques

Enough talk! It's time to see these server-side languages in action. We'll now go through some basic XML processing techniques, with examples from each language. This section should give you a good basic arsenal for tackling the most common XML tasks.

We'll create a simple web application for organizing our CD collection. We'll store the name of the artist, the name of the CD, and the year it was released. The list of CDs will be stored in an XML document. We need to be able to add new CDs to the list, modify existing ones, and also remove CDs from the list. We also want to display the list in HTML format, so we need to be able to transform the XML document to HTML.

The files that we'll create are the following:

cd_list.xml

The XML document containing the list of CDs.

cd_list.xsl

The XSL stylesheet that transforms the CD list to HTML.

cd_list.(asp/php/jsp)

The page that transforms cd_list.xml using cd_list.xsl and outputs the result.

cd_edit.(asp/php/jsp)

The page that allows the user to input/modify information about a specific CD.

cd_save.(asp/php/jsp)

The page that saves the information that was input in cd_edit.(asp/php/jsp).

cd_delete.(asp/php/jsp)

The page that deletes a specific CD from the cd_list.xml.

The XML Document

The XML document that contains the CD list is very simple. Each <cd> element contains three elements: <name>, <band>, and <year>. Following is an example instance (cd_list.xml in the code download):

<?xml version="1.0" encoding="UTF-8"?>

<cd-list>

  <cd id="1">

    <name>Kid A</name>

    <band>Radiohead</band>

    <year>2000</year>

  </cd>

  <cd id="2">

    <name>OK Computer</name>

    <band>Radiohead</band>

     <year>1997</year>

  </cd>

  <cd id="3">

    <name>Felt Mountain</name>

    <band>Goldfrapp</band>

    <year>2001</year>


  </cd>

  <cd id="4">

    <name>Tourist</name>

    <band>St. Germain</band>

    <year>2000</year>

  </cd>

  <cd id="5">

    <name>The Private Press</name>

    <band>DJ Shadow</band>

    <year>2002</year>

  </cd>

  <cd id="6">

    <name>Protection</name>

    <band>Massive Attack</band>

    <year>1994</year>

  </cd>

</cd-list>

Note that each <cd> element has a unique id. We'll put this id in the query string to send it between pages, to identify which CD we are currently working with. For those of you who don't know, the query string is the part after the "?" in the URL. We can send variables using the syntax "name=value", and separate multiple name/value pairs with "&". For example http://myserver.com/mypage.html?param1=value1&param2=value2.

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.