CopperSpice API  1.9.1
XML Namespaces

Using the CsXml library requires a basic understanding of XML namespaces. The following is only a brief introduction.

Namespaces are a were introduced into XML to allow a modular design to assist with resolving naming conflicts within XML documents. Namespaces are defined using a URI like http://www.example.com/placeholder/book/. This does not mean data is available at this address, the URI is simply used to provide a unique name.

Example 1

There are three different places where the name title is used. When processing this document you will encounter problems because every use of title should be displayed in a different manner. The solution is to have some way of identifying the first occurrence as the "title of a book" and distinguish it from the "chapter title".

<document>
<book>
<title>A Brief History of Time</title>
<author title="CBE" name="Stephen Hawking"/>
<chapter>
<title>Our Picture of the Universe</title>
</chapter>
</book>
</document>

Example 2

In the following example the prefix book is a namespace which is applied to the title tag. Before a namespace can be used it must be declared.

<document xmlns:book = "http://example.com/placeholder/book/"> <!-- opens a document tag, declares a namespace -->
<book:title>A Brief History of Time</book:title>

Example 3

A namespace is declared with "xmlns: followed by some text". This namespace can then be applied to elements and attributes by using the text followed by a colon as a prefix. In this example there are two declared namespaces. The first one uses a prefix of book and is then used by specifying book:title or book:author. The default namespace is used for bookX and chapterX.

The default namespace applies only to elements without a prefix. There is no default namespce for an attribute.

<document xmlns:book = 'http://example.com/xml/book/'
xmlns = 'http://example.com/xml/default/' >
<bookX>
<book:title>A Brief History of Time</book:title>
<book:author xmlns:placeholder = 'http://example.com/xml/default/'
title="CBE"
placeholder:title="FRSA"
name="Stephen Hawking"/>
<chapterX>
<title>Our Picture of the Universe</title>
</chapterX>
</bookX>
</document>

The placeholder namespace has the same namespace URI as the default namespace. The following explains why this namespace was added in the book:author element.

  • attributes without a prefix do not belong to any XML namespace, not even to the default namespace
  • omitting the prefix would lead to a conflict
  • writing it as xmlns:title would declare a new namespace with the prefix title instead of applying the default xmlns namespace

More information on XML namespaces can be found at http://www.w3.org/TR/REC-xml-names/.

Conventions Used in the CopperSpice XML Documentation

The following terms are used to distinguish the parts of names within the context of namespaces.

qualified name
Name as it appears in the document, in the above example book:title is a qualified name
namespace prefix
In a qualified name it is the part to the left of the colon, book is the namespace prefix in book:title
local part or local name
Appears to the right of the colon, title is the local part of book:title
namespace URI
Uniform Resource Identifier is a unique identifier for a namespace, it is similar to a URL but does not require data to be accessible by the given protocol at the named address

Elements without a colon like chapterX do not have a namespace prefix. In this case the local part and the qualified name are identical.