*

VB Database: Challenge Lab Week 3

Since you have an idea of what the data structures are for our china shop, it's time to take a side-trip over to XML-land. 

The easiest way to deal with XML is to remember that it's mainly about data: XML's function is to describe (store, carry and exchange) data, and to focus on what data is.  XML itself does not do anything; it's a way to do something.  And XML cares only about data, not about presentation.

In order to send, receive or display the data that XML is storing/carrying/exchanging, a program would have to written. We'll use Internet Explorer 5.0 (and up) to display this week's output.

Here's what a sample XML document looks like.  This came from a database with a table called Employees.  The table has two fields, Name and Salary:

<?xml version="1.0"?>

<employees>
    <employee>
        <name>Stuart Munson</name>
        <salary>23500</salary>
    </employee>
    <employee>
        <name>Robert Brown</name>
        <salary>51000</salary>
    </employee>
    <employee>
        <name>Carol Philips</name>
        <salary>45000</salary>
    </employee>
    <employee>
        <name>James Smith</name>
        <salary>72000</salary>
    </employee>
</employees>

It's a normal text document that has been saved as employees.xml.  You open it up in IE5.x/6 and it will look like this:

Employees.xml as it looks in IE5.5

Not terribly different than the code inside the text file.  About all IE contributes is the handy little expand/contract plus/minus signs, and some color coding and bolding.  (We'd have to apply a style sheet in order to make it look pretty, but don't worry about that right now.)

The first line, <?xml version="1.0"?>, is the XML Declaration.  It is a processing instruction and that's why it has a special syntax. This section of the document is called the Prolog. This area contains metadata, which is data about data, in this case, information about the data contained in the root and child elements.

The next line, <employees>, is the Start Tag of the Root Element.  This section is the actual data and consists of a single element called the root element. The root element contains all of the other elements and attributes in the document.  You'll see that the End Tag of the root element, </employees>, is the very last line of the document.  So, back to our database table, the root element is essentially the table name.

XML data is hierarchical.  Nested in the root element are child elements, <employee>, which have their own child elements, <name> and <salary>. The top-most child element represents a record/row and its child elements represent fields or columns. 

Elements can contain nested elements, text or both.  The "employee" element contains only nested elements.  The "name" and "salary" elements contain text. 

It's possible that an element may contain neither child elements or text - these are called Empty elements and have a special syntax that combines the start and end tags.  Let's say we had an element called permanent_staff.  For now it has no data associated with it.  We would write it out like this:

<permanent_staff />

(Those of you with some exposure to XHTML will see a similarity between how you are to handle tags like <br /> and <img /> that don't have end tags in HTML and how the empty element is written.)

There are also attributes, but we'll save those for another time.  Much ink and many pixels have been expended on whether to use attributes or elements.  (I fall more into the element camp, personally.)

XML is a very picky languge.  Much, much more picky than HTML.  It is absolutely unforgiving if you violate any rules.  An XML document is said to be a well-formed XML document if it follows these rules:

  • Single, unique root element (Like the Highlander, "there can be only one")
  • Matching start/end tags
  • Consistent capitalization (and spelling! ;-) - I use all lower case
  • Correctly nested elements (no overlapping elements)
  • Attribute values enclosed in quotes
  • No repeating attributes in an element

Incidentally, there is a difference between "well-formed" and another term called "valid", but that, too, will be saved for another time.

Only a well-formed document can be displayed in IE5.x/6. If we change the capitalization of the start tag of the first "name" element to proper case, but don't change the end tag, IE tells us this:

What happens if you anger XML

The element names themselves are subject to rules, as well:

  • Should be short and simple
  • Use the naming rules of your database for the elements in the XML documents.
  • Any name can be used, no words are reserved, but the idea is to make names descriptive. Names with an underscore separator are nice.
  • Can contain letters, numbers, and the underscore (also the period, colon, and dash, but don't use those - see below)
  • Must not start with a number or punctuation character
  • Must not start with the letters xml (or XML or Xml ..)
  • Cannot contain spaces
  • Non-English letters like йтб are perfectly legal in XML element names, but watch out for problems if your software vendor doesn't support them.
  • Avoid "-" and "." because these have other meanings (subtraction and object.property/method)
  • The ":" should not be used in element names because it is reserved to be used for something called namespaces (which, guess what, we're going to worry about later...)

So...

All that being said, your assignment is to take the China Shop database you created (or downloaded) and turn those tables into XML documents. 

(Please ask questions if I wasn't clear enough :-)

Note:  Access 2002 has the ability to save a table as XML.  Don't do that for this exercise.  Create the XML file from scratch.  (Just remember copy and paste is your friend :-)

Download Proposed Solution



Internet Content Rating Association Valid CSS!

Alrak's Course Resources ©2002-2007 Karla Carter. All rights reserved. This material (including, but not limited to, Mini-lectures and Challenge Labs) may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder. For permission, contact Karla.