XML: Attributes

Posted in XML on June 16th, 2009 by Chris H. – Be the first to comment

Digging In

This is a continuation of my earlier article, XML: An Introduction, and is meant to go into XML a bit deeper than the previous article. Please read that article first if you have not yet done so, or this article may not make much sense.

Attributes

Taking a cue from the last article we’ll start with some sample HTML which will help explain things a bit better:

<html>
<head>
<title>My Sample Page</title>
</head>
 
<body>
<img src="helloworld.jpg" height="50" width="200">
</body>
</html>

In the preceding example the ‘img’ tag has three attributes (src, height, and width). Well XML is not any different. It supports attributes as well.

Starting with the code from the last example from the previous article:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<vehicles>
  <vehicle>
    <make>Ford</make>
    <model>Mustang</model>
    <year>2009</year>
    <price>$33,049.99</price>
  </vehicle>
  <vehicle>
    <make>Chevy</make>
    <model>Camaro</model>
    <year>1968</year>
    <price>$75,000</price>
  </vehicle>
  <vehicle>
    <make>Dodge</make>
    <model>Charger</model>
    <year>2007</year>
    <price>$26,743</price>
  </vehicle>
  <vehicle>
    <make>Pontiac</make>
    <model>Solstice</model>
    <year>2008</year>
    <price>$18,999.98</price>
  </vehicle>
</vehicles>

Lets say that we wanted to condense this code. We have a list of 5,000 cars, and listing them like this will get quite lengthy. We want something more concise and easier to read or scan.

We can modify our XML (using attributes of course) to get a more concise listing:

1
2
3
4
5
6
<vehicles>
  <vehicle make="Ford" model="Mustang" year="2009" price="$33,049.99" />
  <vehicle make="Chevy" model="Camaro" year="1968" price="$75,000" />
  <vehicle make="Dodge" model="Charger" year="2007" price="$26,743" />
  <vehicle make="Pontiac" model="Solstice" year="2008" price="$18,999.98" />
</vehicles>

The code has gotten a lot shorter (and easier to read) as you can see. Going from 26 lines to just 6.

That concludes this article, but I will be following up shortly with a few posts on using XSL to process our newly written XML.

XML: An Introduction

Posted in XML on June 9th, 2009 by Chris H. – 2 Comments

Intro

Over the past few years I have tried here and there to learn XML and what it has to offer. I had been programming for many years prior to my introduction with XML and I figured it’d be just like any other language. I was wrong.

My issue with XML was one I would like to convey to you today in the hopes that it will become easier for you to pick up this wonderful language. As stated above I had programmed in various languages (C/C++, Perl, Tcl/Tk, PHP, JavaScript, etc.) before deciding that XML was something I wanted to look into learning and perhaps utilizing for some purpose.

Having come from these other languages, languages which brought with them a very clear defined syntax, structure, and order, I found myself lost when it came to learn the biggest secret (and my biggest issue) of XML. Nothing is pre-defined. XML is a free-form language provided you follow its very basic rules.

I want to say up front that my goal here is not to give you a step by step tutorial on how to write and use XML, but rather I’d like to introduce you to XML’s concepts and flexibility. In later posts I will dive in a bit deeper and explain the processing side of things.

The First Steps

Lets start with a simple XML document and dissect its meaning:

1
2
3
4
5
6
<vehicle>
  <make>Ford</make>
  <make>Chevy</make>
  <make>Dodge</make>
  <make>Pontiac</make>
</vehicle>

Those of you who know HTML know that this syntax is very similar. The main difference with XML is that the tags above, and again the issue I had the most issue dealing with is that they mean nothing.

Using HTML as an example:

1
2
3
4
5
6
7
8
9
<html>
<head>
<title>My Sample Page</title>
</head>
 
<body>
Hello World!
</body>
</html>

All of the elements have pre-defined meanings (as published by the W3C). In the HTML example, the <title> tag tells your web browser to display whatever is in between the opening and closing tags in the title bar of your browser window. For XML the story is quite different.

I stated above that XML is more of a free-form language than one defined by someone else. What this means is that inside your XML document, <title> could be the title of your website, but it could also be the title of a book, title of a document, title to your car, etc.

Consider the following code from our example earlier:

1
2
3
4
5
6
<vehicle>
  <make>Ford</make>
  <make>Chevy</make>
  <make>Dodge</make>
  <make>Pontiac</make>
</vehicle>

…which by itself isn’t very descriptive (or helpful).

Let’s fix up the code a bit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<vehicles>
  <vehicle>
    <make>Ford</make>
    <model>Mustang</model>
    <year>2009</year>
    <price>$33,049.99</price>
  </vehicle>
  <vehicle>
    <make>Chevy</make>
    <model>Camaro</model>
    <year>1968</year>
    <price>$75,000</price>
  </vehicle>
  <vehicle>
    <make>Dodge</make>
    <model>Charger</model>
    <year>2007</year>
    <price>$26,743</price>
  </vehicle>
  <vehicle>
    <make>Pontiac</make>
    <model>Solstice</model>
    <year>2008</year>
    <price>$18,999.98</price>
  </vehicle>
</vehicles>

In the preceding example we have expanded our code from our original example to something a lot more usable for later processing. We now have a listing of cars with which we may process various bits of information from in our application of choice which supports XML input. This could be an inventory system (if by chance you’re a car dealership), or this could just simply be personal documentation on the cars in your garage. It really could be anything which is the whole point as to what I have been saying.

I want to conclude this short introduction for now, but I will continue this series with more posts soon. XML is truly amazing for the simple fact that you can map and/or describe anything your mind can think of, and output it in any number of ways…even as a web page!

The Zen Garden is Here!

Posted in Misc. on June 9th, 2009 by Chris H. – Be the first to comment

5 years after creating this domain, a website is finally launched here. Working on some content now so come back soon! :)