Declaring Namespaces




To declare a namespace, you need to be aware of the three possible parts of a namespace declaration:

  xmlns— Identifies the value as an XML namespace and is required to declare a namespace and

can be attached to any XML element.

  prefix —Identifies a namespace prefix. It (including the colon) is only used if you’re declaring

a namespace prefix. If it’s used, any element found in the document that uses the prefix
(prefix:element) is then assumed to fall under the scope of the declared namespace.

  namespaceURI— It is the unique identifier. The value does not have to point to a Web resource;

it’s only a symbolic identifier. The value is required and must be defined within single or double
quotation marks.

There are two different ways you can define a namespace:

  Default namespace —Defines a namespace using the xmlns attribute without a prefix, and all

child elements are assumed to belong to the defined namespace. Default namespaces are simply
a tool to make XML documents more readable and easier to write. If you have one namespace
that will be predominant throughout your document, it’s easier to eliminate prefixing each of
the elements with that namespace’s prefix.

  Prefixed namespace —Defines a namespace using the xmlns attribute with a prefix. When

the prefix is attached to an element, it’s assumed to belong to that namespace.

Default namespaces save time when creating large documents with a particular
namespace; however, they don’t eliminate the need to use prefixes for attributes.

The following example demonstrates the use of default namespaces and prefixed namespaces.

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Book List</title>
</head>
<body>

<blist:books

xmlns:blist=”http://www.wrox.com/books/xml”>
<blist:book>

<blist:title>XSLT Programmers Reference</blist:title>
<blist:author>Michael Kay</blist:author>

</blist:book>

</blist:books>

</body>
</html>


learn guitarphysics learnteliphonyxmlphysicsenjoylife