Learn XML Programming

Section 4:  Working With XML

What are XML namespaces, and how are they used?

XML namespaces are useful little pieces of programming that are used to make your elements unique among the entire world of XML pages.  They allow you to specify a URI, or Uniform Resource Identifier, such as an internet address, and define your elements in such a way that the browser will be able to see that your elements are unique from those that your friend Bob is using on his page about his cats, Tudor and Shady.

To create a namespace, you have to define it. This is done using the following format:

<namespace:element xmlns:namespace=”unique_URI”>

</ namespace:element>

You also need to apply the namespace to any attributes or elements contained within the particular element that you’re naming.

An example of a namespace in a file would be as follows:

<my_cats:cats_info xmlns:my_cats=>

<my_cats:cats>Tooter and Shade are the best cats in the world!</ my_cats:cats>

<my_cats:friends>Loki and Sally are pretty cool cats as well, though.</ my_cats:friends>

</ my_cats:cats_info>

When a browser runs across this code, you can be sure that it’s not going to mistake anything inside of <cats_info> for information defined on any other site.  The namespace makes sure that it’s unique across the entire Internet by linking it to your URL (though the URI that you use doesn’t have to be the site’s URL.)

What about XML schemas?  What are they?

A schema is a collection of validation rules that are designed to constrain and manage data values in XML.  The schema is programmed into your XML file, using the regular XML syntax… though you will have to learn a bit of schema vocabulary, there are no additional languages that you need to use.

Schemas help you to perform some functions within your file, including functions involving integers, dates, and times.  This can be quite useful, as many of the alternatives to schemas are unable to handle these specific forms of data.  On the down side, though, there is much less support for schemas available than there is for some other XML technologies… the schema is newer than some of the others, and while more support is created for schemas every day it still has a way to go before it catches up with its predecessors.

Ok, so what is XML-FO?

XML-FO is a special type of namespace that is used by XSL to format objects.  (Get it? FO?  Format Object?  Yeah, that’s what I thought, too.)  XML-FO is used to help control the generation of XML documents by XML processors, and is responsible for creating custom layouts and other XML effects.

Of course, XML-FO is only half of the story… XSL uses it along with XSLT to create transitions, format pages, and transform XML pages into other languages.  XSLT (which stands for “XSL Transitions”) is used to connect the various elements in a meaningful way, whereas XML-FO is used to place the elements dynamically on the screen.

An example of XML-FO in action would look like this, defining the layout of three different pages (cats, cat_friends, and cat_enemies):

<?xml version=”1.0″?>



<fo:simple-page-master master-name=”cats”








<fo:simple-page-master master-name=”cat_friends”








<fo:simple-page-master master-name=”cat_enemies”









Notice how each page defines the height, width, and margins so that the XML document will know the exact parameters that it’s working inside of.

How do you process XML?

XML can be processed in several different ways, using a variety of processors.  Regardless of the processor used, though, the job of the processor remains the same: take the data within an XML document, combine it with data from outside sources (such as CSS or XSL files), and present that information to the application that’s requesting the data (often the XML parser that’s built into modern browsers.)

For the casual XML programmer, the processor isn’t as big a deal as one might think.  You won’t be dealing with the processor directly, and you certainly won’t be programming your own processor… instead, you simply will provide the XML code for the processor to use, and make sure that the code is written correctly so as to avoid any errors that the processor might encounter.

In case you’re wondering how you access the processor, it’s something you’ve been doing this whole time:

Your declaration alerts the browser or other program that the file it’s seeing is written in XML, and it diverts it to the processor from there.