Basic Techniques - Page 20
November 18, 2002
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¶m2=value2.
JSP - Page 19
Practical XML for the Web
Transforming the XML - Page 21
|