Prefix Binding
Home ] Up ] Namespace Syntax ] [ Prefix Binding ] Default Namespaces ]

 

 

Each prefix in a qualified name must be associated with a namespace URI. Namespace URIs do not necessarily refer to an actual, existing document or web page.  They don't even have to use the http protocol, although that is the most common protocol to use.

Prefixes are bound to namespace URIs by adding an xmlns:prefix attribute to the prefixed element or one of its ancestors.

For example:

<?xml version="1.0" encoding="utf-8"?>
<review:book-review xmlns:review="http://www.book-review.com/">
  <author>Harsh K. Critic</author>
  <date>5 Nov 2006</date>
  <book>A Brief History of the Twenty-First Century</book>
  <comments>
  This book should be read by every American.
  Blah, blah, blah...
  </comments>
</review:book-review>

Another example is the namespace for XHTML:

<html xmlns:xhtml="http://www.w3.org/1999/xhtml">

Perhaps the most common place to specify an XML namespace binding is on the root element of an XML document.

You can declare multiple XML namespace bindings in a single element.

Because such bindings have scope within the element in which they are declared and its contents, the above review prefix is associated with the book-review element and also its child elements.

Let's combine the book and review vocabularies to produce the following example:

<?xml version="1.0" encoding="utf-8"?>
<book-reviews xmlns:reviews="http://reviews.info/"
              xmlns:books="http://books.info/">
  <reviews:author>Harsh K. Critic</reviews:author>
  <reviews:date>5 Nov 2006</reviews:date>
  <books:book>
    <books:title>The World is Flat</books:title>
    <books:subtitle>A Brief History of the Twenty-First Century</books:subtitle>
    <books:author>Thomas Friedman</books:author>
    <books:publisher>Farrar, Straus and Giroux</books:publisher>
    <books:date-published>April 30, 2006</books:date-published>
    <books:isbn>0374292795</books:isbn>
    <books:pages>593</books:pages>
    <books:price>30.00</books:price>
  </books:book>
  <reviews:comments>
  This book should be read by every American.
  Blah, blah, blah...
  </reviews:comments>
</book-reviews>

To see how your browser renders this XML, click here.

 
The page was last updated February 19, 2008