LanguagesXMLXML for Beginners, Part 2: Creating and Maintaining Structure

XML for Beginners, Part 2: Creating and Maintaining Structure

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Preface

I have authored numerous online articles on XML.  These articles cover the waterfront from introductory to advanced.  I maintain a consolidated index of hyperlinks to all of my XML articles at my personal Web site so that you can access earlier articles from there.


Introduction

As I discussed in an earlier article, I am writing a series of articles explaining XML for beginners.  I am being particularly careful to avoid the use of technical jargon (or to explain it when I use it).

Experts skip this article

Those of you who already know a lot about XML can skip ahead to something more challenging, such as some of my articles on XSL.  You will find links to all of my articles at my personal Web site.

Beginners, keep reading

Those of you who are just getting your feet wet in this area (and may have found the XML water to be a little deep), keep reading.

What is XML?

In an earlier article, I provided the following brief description of XML.
 

XML gives us a way to create and maintain structured documents in plain text that can be rendered in a variety of different ways.

A primary objective of XML is to separate content from presentation.

Tell me in plain English

Then I proceeded to break down the jargon into plain English and provided some examples of structured documents.

I made a promise

At the end of that article, I promised that this article would discuss the mechanism by which XML, in conjunction with a rendering engine such as XSL, uses plain text to display richly-formatted structured documents.

Achieving Structure

So, just how does XML use plain text to create and maintain structure?

Consider the following simple structure that represents a book.  (This book certainly wasn’t written by me, because it is much too brief.)

The book described by this structure has two chapters with some text in each chapter.
 

Begin Book

    Begin Chapter 1
        Text for Chapter 1
    End Chapter 1

    Begin Chapter 2
        Text for Chapter 2
    End Chapter 2

End Book

A simple example

A real book obviously has a lot more structure than this.  For example, a typical book will probably have a Forward or a Preface.  A typical book will usually have a Table of Contents.

Breaking the structure down further produces paragraphs within the text, words within the paragraphs, etc.  Also, a book will frequently have an Alphabetical Index.

However, I am trying to keep this example as simple as possible, so I left those things out.

A primary objective

In the earlier description, I told you that “A primary objective of XML is to separate content from presentation.”

This separation, and the fact that the XML document is maintained in plain text, makes it possible to share the same physical document among different computers in a way that they all understand.  (This is often not true, for example, for documents that are maintained in the proprietary formats of word processing software.)

Many different computers and operating systems

This is no small accomplishment. Over the years, dozens of different types of computers have been built, operating under different operating systems, and running thousands of different programs.

As a result, the modern computer world is often like being on an island where everyone speaks a different language.

A common language for structured documents

XML attempts to rectify this situation by providing a common language for structured documents.

What Does XML Contribute?

I am going to ease into the technical details later.  At this point, suffice it to say that XML provides a definition of a simple scheme by which the structure and the content of a document can be established.

Even I can understand an XML document

The resulting physical document is so simple that any computer (and most humans) can read it with only a modest amount of preparation.

XML is a meta language

You will sometimes see XML referred to as a “meta” language.

What does meta mean?

In computer jargon, the term meta is often used to identify something that provides information about some other information.

Stock and bond price information

For example, consider the listings of stock prices, bond prices, and mutual fund prices that commonly appear in most daily newspapers.

The various tables on the page provide information about the bid and ask prices for the various stock, bond, and mutual fund instruments.

What you need is meta information

But, how do you read those charts?  How do you extract information from the charts?  You need some information about the information contained in the charts.  You need some meta information.

Stock and bond meta information

Usually somewhere on the page, you will find an explanation as to how to interpret the information presented throughout the remainder of the page.

You could probably think of the information contained in the explanation as meta information. It provides information about other information.

What about the alphabetical index in a book?

Is the alphabetical index of a book a form of meta information?  Probably so.

For example, the alphabetical index can tell you if the book contains information about XML or other topics of interest to you.  If so, it will tell you where in the book you can find that information.

The index can also tell you where to find information about elements and attributes that I will discuss later.  So, yes, in my opinion, the alphabetical index in a book provides meta information.

So, why might people refer to XML as a meta language?

If you write a book and maintain its content in XML, XML doesn’t tell you how to structure the document that represents your book.

XML provides a set of rules for structuring

Rather, XML provides you with a set of rules that you can use to establish your own structure and content when you create the document that represents your book.

XML is not the language that you use to establish the structure and content of your book.  Rather, XML tells you how to create your own language for creating structure and maintaining content.

It is up to you to decide how you will use those rules to define your own language for establishing the structure and content of your book.

Invent your own language

You might say that XML is a language that provides information about a new language that you are free to invent.

Does everyone use a different language?

As it turns out, different groups of people having common interests have gotten together and have used XML to invent a common language by which the persons in the group can create, maintain, and exchange document structure in their areas of interest.

The Chemical Markup Language

For example, a group of chemists has gotten together and has invented a common language by which they create and exchange structured documents on chemistry.

MathML

Similarly, a group of mathematicians has gotten together and has invented a common language by which they create and exchange structured documents on mathematics.

XML is easily transported

If you follow the rules for creating an XML document, then the document that you create can easily be transported among various computers and rendered in a variety of different ways.

Two different renderings

For example, you might want to have two different renderings of your book. One rendering might be in conventional printed format and the other rendering might be in an online format.

No requirement to modify the XML source document

The use of XML makes it practical to render your book in two or more different ways without any requirement to modify the original document that you produce.

Finally, the name is revealed

This leads to the name: eXtensible Markup Language or XML.

Applying XML

Before I wrap up this article, I am going to provide two different XML examples, either of which might reasonably represent the simple book presented earlier.

The first example follows:
 

<book>
    <chap>
        Text for Chapter 1
    </chap>

    <chap>
        Text for Chapter 2
    </chap>
</book>

This example shows typical XML syntax.

Compare with earlier book description

If you compare this example with the informal book example given earlier, you should  see a one-to-one correspondence between the “elements” in this XML document and the informal description of the book presented earlier.

An improved example

The following example provides a modest improvement by including an “attribute” named number in each of the chapter elements.  This attribute contains the chapter number and is part of the information that defines the structure of the book.
 

<book>
    <chap number=”1″>
        Text for Chapter 1
    </chap>

    <chap number=”2″>
        Text for Chapter 2
    </chap>
</book>


What’s Next?

In the next article, I will continue this discussion, and will tell you the meaning of the terms element and attribute.

Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without  express written permission from Richard Baldwin is prohibited.


About the author:

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories