Home arrow XML arrow Page 5 - XML Basics (part 2)

The Name Game - XML

Now that you know the basics, this article explains how to use XML's more advanced constructs to author complex XML documents. Entities, namespaces, CDATA blocks, processing instructions - they're all in here, together with aliens, idiots, secret agents and buried treasure.

  1. XML Basics (part 2)
  2. Splitting Up
  3. Eating Humble PI
  4. XML And Alcohol
  5. The Name Game
  6. An Entity In The Attic
  7. Digging For Treasure
  8. The Man From IDIOT
  9. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 5
July 28, 2001

print this article


Setting up a namespace is simple - here's the syntax:

<elementName xmlns: prefix="namespaceURL">
In this case, the prefix is the unique string used to identify the namespace; this is linked to a specific namespace URL.

A namsepace is usually declared at the root element level, although authors are free to declare it at a lower level of the tree structure too.

Once the namespace has been declared within the document, it can be used by prefixing each element within that namespace with the unique namespace identifier. Take a look at my revised stock portfolio, which now uses a "mytrades" namespace to avoid name clashes.

<mytrades:portfolio xmlns:mytrades="http://www.somedomain.com/namespaces/mytrades/"> <mytrades:stock>Cisco Systems</mytrades:stock> <mytrades:stock>Nortel Networks</mytrades:stock> <mytrades:stock>eToys</mytrades:stock> <mytrades:stock>IBM</mytrades:stock> </mytrades:portfolio>
And once Tom gets over his hangover, I guess he'd find it pretty easy to fix his document too.

<?xml version="1.0"?> <toms_store:inventory xmlns:toms_store="http://www.toms_store.com/"> <toms_store:category>Mice</toms_store:category> <toms_store:item>Mouse C106</toms_store:item> <toms_store:vendor>Logitech</toms_store:vendor> <toms_store:stock>100</toms_store:stock> <toms_store:category>Handhelds</toms_store:category> <toms_store:item>Visor Deluxe</toms_store:item> <toms_store:vendor>HandSpring</toms_store:vendor> <toms_store:stock>23</toms_store:stock> <toms_store:category>MP3 players</toms_store:category> <toms_store:item>Nomad</toms_store:item> <toms_store:vendor>Creative</toms_store:vendor> <toms_store:stock>2</toms_store:stock> </toms_store:inventory>
In case you're wondering, the namespace URL is simply a pointer to a Web address, and is meaningless in practical terms; the XML specification doesn't really care where the URL points, or even if it's a valid link.

In case a single document contains two or more namespaces, adding namespace declarations and prefixes to every element can get kind of messy - as the following example demonstrates.

<?xml version="1.0"?> <me:person xmlns:me="http://www.mywebsite.com/"> My name is <me:name>Huey</me:name>. I'm <me:age>seven</me:age> years old, and I live in <me:address>Ducktown</me:address> with <rel:relationships xmlns:rel="http://www.mywebsite.com/relationships/">my brothers <rel:name>Dewey</rel:name> and <rel:name>Louie</rel:name></rel:relationships>. </me:person>
In such a situation, XML allows you to specify any one namespace as the default namespace, by omitting the prefix from the namespace declaration. Modifying the document above to make "me" the default namespace, we have

<?xml version="1.0"?> <person xmlns="http://www.mywebsite.com/"> My name is <name>Huey</name>. I'm <age>seven</age> years old, and I live in <address>Ducktown</address> with <rel:relationships xmlns:rel="http://www.mywebsite.com/relationships/">my brothers <rel:name>Dewey</rel:name> and <rel:name>Louie</rel:name></rel:relationships>. </person>
Namespaces need not be restricted to elements alone - attributes can use namespaces too, as the following example demonstrates.

<?xml version="1.0"?> <mystuff:collection xmlns:mystuff="http://www.somedomain.com/mystuff" xmlns:music="http://www.somedomain.com/music_genres"> <mystuff:artist>Spears, Britney</mystuff:artist> <mystuff:title music:genre="Pop">Oops, I Did It Again!</mystuff:title> </mystuff:collection>

This article copyright Melonfire 2001. All rights reserved.

>>> More XML Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates


Dev Shed Tutorial Topics: