Buzz’s Blog: On Web 3.0 and the Semantic Web

Mar 26 2009   11:31PM GMT

SQL and XML: declarative is exciting

Roger King Roger King Profile: Roger King

In the continuing series of blogs on the Semantic Web and other advanced web technology, we’ve looked at XML as a cornerstone of the technology that allows us to markup data, and in combination with namespaces, create powerful tools for sharing the meaning – and not just the structure – of data. There’s something special about XML that is at the core of its truly amazing widespread adoption, that explains its versatility as the language of choice for tagging data, no matter what the purpose.

What is it?

It’s that XML is “declarative”.

A declarative language is one that allows us to write programs that tell us what needs to be computed, not the order in which primitive operations need to be carried out in order to get the result. Java, C++, JavaScript, C#, Objective C, ActionScript, PHP – none of these are declarative.

Some Non-Declarative Code.

A Here’s some code:

for ( i = 0; i <100; i++ )
stuff[i] = stuff[i] + 1;

It says to start i at 0, then add 1 to i until you get to 99, and each time i is incremented, add 1 to that element in an array called stuff.

This manipulating-an-array program is the classic piece of non-declarative code. It doesn’t just say to add one to every element in an an array, it also tells the order in which to do it. This extra information shouldn’t really be needed, but in non-declarative languages – known as “imperative” languages – it is frequently necessary.

Some Declarative Code.

Now, here is some declarative code. It’s SQL, the universal database language:

SELECT Firstname
FROM Clients
WHERE (Lastname = ‘Smith’) AND (City = “Boulder”) AND (Bday BETWEEN ‘2/10/1970’ AND ‘2/10/1980’)

Clients is a relational “table”, and Firstname, Lastname, City, and Bday are all “attributes” (or “columns”) of that table.

This piece of code gives us the first name of any client whose last name is Smith, and who is from Boulder, and was born between Feb 10 of 1970 and Feb 10 of 1980.

Notice that it tells the computer what data we want, and not the sequence of steps that must be carried out to return the value. We don’t know what order the rows in the table will be examined. We don’t know if the three conditions will all be checked at once, or if we will filter the table first by picking out all clients who are from Boulder.

XML is Declarative.

XML is a declarative language, too. Let’s look at it.

This is the XML from the previous posting of this blog.

<smil xmlns:qt=”
/resources/smilextensions” qt:autoplay=”true” qt:time-slider=”true”>
<meta name=”title” content=”Buzz’s Video”/>
<root-layout background-color=”white” width=”320″ height=”290″/>
<region id=”videoregion” top=”0″ left=”0″ width=”320″ height=”290″/>
<video src=”” region=”videoregion”/>
<video src=”” region=”videoregion”/>

An XML program consists of “elements” and “attributes”. Notice <head> and </head> form the bounds for an element, as do <body> and </body>. Also note that there is an element nested within <head> and it’s marked by <layout> and </layout>. The tags <seq> and </seq> denote an element inside <body>.

The other major construct in XML is called an attribute, and name and content are two attributes with values “title” and “Buzz’s Video”, respectively. Attributes are always simple character values, and therefore cannot be nested.

When this program is saved with the name buzz.smil, and then run by Quicktime, it will download a video from my website (a very nice piece of animation by a student named Jochen Wendel), and then play it twice in succession. See the previous blog for more of an explanation of how SMIL works. It also discusses the difference between XML and its extensions, such as SMIL.

Note that these tags are not part of XML itself; rather they are part of the namespace that has been defined for the SMIL extension of XML. This illustrates the power of XML: it can be used to define other languages.

To understand the XML above, all that is needed is access to the SMIL namespace (which is available at the URL listed at the beginning of the code), and a program that knows how to interpret XML that contains these tags. In this case, it defines a layout for the screen, and that a video should be played twice, sequentially. Quicktime has been programmed to understand the elements and attributes of the SMIL XML language.

Going from “sequential” to “parallel”.

To make our point stronger, here’s a variation. Instead of playing the two videos sequentially, I am using the <par> and </par> tags that represent “parallel” in the SMIL namespace. I have also made the layout area twice as big, and broken it up into two regions. Now, the program plays the video twice, side-by-side, one in each region. At the bottom of this blog entry is what you should see if you save it as buzz.smil and run it with Quicktime. There is also a nice soundtrack.

<smil xmlns:qt=”
/resources/smilextensions” qt:autoplay=”true” qt:time-slider=”true”>
<meta name=”title” content=”Buzz’s Video”/>
<root-layout background-color=”white” width=”640″ height=”290″/>
<region id=”videoregion” top=”0″ left=”0″ width=”320″ height=”290″/> <region id=”videoregion2″ top=”0″ left=”320″ width=”320″ height=”290″/>
<video src=”” region=”videoregion”/>
<video src=”” region=”videoregion2″/>


Notice that this program, written in the SMIL extension of XML, is quite declarative: it says to create a layout, break it into two regions, and then place the animation (a .mov video) in both regions, in parallel. It does not say how to do this. The program doesn’t specify the sequence of steps that are needed to get the job done – rather, what the result should look like.

This makes XML programs far easier to read than programs in imperative languages, thus making the programs easier for a programmer to write, and easier for another programmer to read and perhaps change later on. This makes programs in XML far more likely to be written correctly and then used appropriately.

We’ll look at declarative languages again, in future entries of this blog.

An SMIL XML program that plays a video twice, in parallel.
An XML program that plays a video twice, in parallel.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: