XML Tutorial/Namespace/Namespace

Материал из Web эксперт
Версия от 21:22, 25 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Change URI of namespace

<person xmlns="http://www.wbex.ru"> 
    <paragraph xmlns="http://www.w3.org/1999/xhtml">This is XHTML</paragraph> 
http://www.wbex.ru is the default namespace for the document as a whole, 
http://www.w3.org/1999/xhtml is the default namespace for the <paragraph> element, and any of its descendants. 
http://www.w3.org/1999/xhtml namespace overrides the http://www.wbex.ru namespace.

Declaring Namespaces on Descendants

You don"t have to declare all of your namespace prefixes on the root element.
A namespace prefix can be declared on any element in the document. 
<person xmlns="http://www.wbex.ru"> 
    <xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
        This is XHTML

Default Namespaces

A default namespace is like a regular namespace.
You don"t have to specify a prefix for all of the elements that use it. 
<person xmlns="http://www.wbex.ru"> 
You can declare more than one namespace for an element, but only one can be the default. 
<person xmlns="http://www.wbex.ru" 
    <xhtml:p>This is XHTML</xhtml:p> 

How XML Namespaces Work

To use XML namespaces, elements are given qualified names. 
Qualified name is abbreviated to QName.
These qualified names consist of two parts.
The local part is the same as the names we have been giving elements.
The namespace prefix specifies to which namespace this name belongs. 
For example: 
<pers:person xmlns:pers="http://www.wbex.ru"/> 
xmlns stands for XML Namespace. 
pers is the namespace prefix.
http://www.wbex.ru is the URI of the namespace. 
Prefix itself (pers) doesn"t have any meaning - its only purpose is to point to the namespace name. 
The prefix is needed on both the start-tags and end-tags of the elements. 
The elements are no longer simply being identified by their names, but by their QNames. 
This prefix can be used for any descendants of the <pers:person> element to denote that they also belong to the http://www.wbex.ru namespace.
<pers:person xmlns:pers="http://www.wbex.ru">

Understanding URIs

A URI (Uniform Resource Identifier) is a string of characters that identifies a resource. 
URI could be URL (Uniform Resource Locator), or URN (Universal Resource Name). 
The first part of the URL specifies the protocol.
For example, here"s a URL to a web page on the Internet: http://www.google.ru/intl/en/about.html 
A URN looks something like this: urn:foo:a123,456 
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

  <xsd:element name="name" type="xsd:string" />
  <xsd:element name="source" type="xsd:string" />

Using Prefixes

URIs must be used for the prefix names. 
A URI (Uniform Resource Identifier) is a string of characters that identifies a resource. 
It can be URL (Uniform Resource Locator) or URN (Universal Resource Name). 
The URL we"re using is simply used as a name, for the namespace.
XML parser won"t try to pull back any resources from that location.
XML parser uses it for naming the namespaces in the document. 
<html xmlns="http://www.w3.org/1999/xhtml">
   <title>Book List</title>
     <pub:title>Mastering XHTML</pub:title>
     <pub:author>Ed Tittel</pub:author>
     <pub:title>Java Developer?Guide to E-Commerce
     with XML and JSP</pub:title>
     <pub:author>William Brogden</pub:author>

Why We Need Namespaces

For the following XML document, 
<?xml version="1.0"?> 
To an XML parser, there isn"t any difference between the two <title> elements.