Writing Our Own ML without a Net: Examples
April 12, 1999
So now we're going to create our own markup language (ML) that
will be used throughout the rest of this article (both parts).
We could start by creating a DTD, but the
DTD syntax is complicated
and not the way some of us naturally think about markup.
Trust me -- we don't need the (DTD) net, at least not yet.
Some of you may recall my fondness for the Beatles from my first \
XML article. Let's create an XML description of a tiny fraction
of my Beatles collection. In fact, we'll call this Collection
Markup Language, or Collection ML for short. Why not Beatles
ML? Because it will have general elements that are not at all
explicitly associated with the Beatles. Why not Record ML?
Because the collection may contain CDs, record albums (for those
of us old enough to remember vinyl), books, and so forth.
(Like most small XML examples, the language will be a bit
contrived, meant more for illustrative purposes than to be
useful in and of itself.)
|
Examples XML documents, DTD, and images |
Several small XML documents needed to follow the remainder
of this article. Install them anywhere you wish, but
subsequent downloads should be in the same location
(folder). |
|
Examples/doingit1.zip
(approx. 54 KB) |
Requires Zip. Depending on your browser, you'll
probably want to do a Save As (Download). If you don't
have zip utilities, download each file separately. |
|
|
After you unzip or download the files, take a close look at
Examples/collection1.xml.
(With some browsers, you may needed to use a text editor to view
the XML file, reproduced below for your convenience.)
A Collection consists of Book and
CD elements in no particular order.
I'd like to be able to enter the information and deal with the
presentation and ordering later (in part 2 of this article).
Most of the elements in Collection ML have few if any attributes.
There's no particular reason for this limitation other than to
keep the example relatively simple. In fact, where I used
attributes as opposed to elements may seem a bit arbitrary;
this will be covered in the section
To E or Not To E: Elements vs.
Attributes.
Also, except for Collection, Book,
CD, and Chart, the elements are not
very hierarchical; only a few elements can contain other elements.
However, this is arguably a reasonable way to express the Book
and CD information, so we won't be too harsh on the language
creator. (The Owner element and owner
attribute of Collection refer to the same thing,
so one is redundant.) The Book element has optional
nested elements Notes and Rating.
The Label element has an optional attribute called
country. Peak has optional elements
weeks and country.
<?xml version="1.0" standalone="yes" ?>
<!-- A Collection consists of Book and CD elements
in no particular order. -->
<Collection owner="Ken Sall" location="nevermind">
<Owner>Ken Sall</Owner>
<Book>
<Title>Complete Beatles Chronicle, The</Title>
<Author>Lewisohn, Mark</Author>
<Type>Chronology</Type>
<Published publisher="Harmony Books">
1992</Published>
<Rating>5 stars</Rating>
<Notes>
Covers the years 1957 through 1970. No solo info.
Great appendices with chart info, discography,
composer index,radio, tv, and live performances,
and much more.
</Notes>
</Book>
<CD>
<Title>Band on the Run</Title>
<Artist>McCartney, Paul and Wings</Artist>
<Chart>
<Peak weeks="4">1</Peak>
<Peak country="UK">1</Peak> <!-- guess -->
</Chart>
<Type>Rock</Type>
<Label>Capitol</Label>
<Label country="UK">EMI</Label>
<AlbumReleased>1973</AlbumReleased>
<Remastered format="gold CD">1993</Remastered>
<Remastered format="2 disc box set with booklet">
1999</Remastered>
</CD>
<CD>
<Title>Venus and Mars</Title>
<Artist>McCartney, Paul and Wings</Artist>
<Chart>
<Peak weeks="1">1</Peak>
<Peak country="UK">2</Peak> <!-- guess -->
</Chart>
<Type>Rock</Type>
<Label>Capitol</Label>
<Label country="UK">EMI</Label>
<AlbumReleased>1975</AlbumReleased>
<Remastered format="gold CD with 3 bonus tracks">
1994</Remastered>
</CD>
<Book>
<Title>Many Years From Now</Title>
<Author>McCartney, Paul</Author>
<Type>Autobiographical</Type>
<Published publisher="Henry Holt and Company">
1997</Published>
<!-- Notice the absence of Notes and
Rating elements. I haven't read this book yet.
This illustrates some optional elements that
are children of Book element.
-->
</Book>
</Collection>
What's It All About, Ælfred?
Doing It With XML, Part 1
To E or Not To E: Elements vs. Attributes
|