XML Tutorial/Namespace/Namespace

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

Change URI of namespace

<person xmlns="http://www.wbex.ru"> 
    <name/> 
    <paragraph xmlns="http://www.w3.org/1999/xhtml">This is XHTML</paragraph> 
</person> 
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"> 
    <name/> 
    <xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
        This is XHTML
    </xhtml:p> 
</person>


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"> 
    <name> 
        <title>Sir</title> 
    </name> 
</person> 
You can declare more than one namespace for an element, but only one can be the default. 
<person xmlns="http://www.wbex.ru" 
        xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
    <name/> 
    <xhtml:p>This is XHTML</xhtml:p> 
</person>


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">
  <pers:name>
    <pers:title>Sir</pers:title>
  </pers:name>
</pers:person>


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"
  targetNamespace="http://www.wbex.ru" 
  xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">

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


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">
  <head>
   <title>Book List</title>
  </head>
  <body>
   <pub:publications
   xmlns:pub="http://www.wbex.ru/namespaces/pub">
    <pub:book>
     <pub:title>Mastering XHTML</pub:title>
     <pub:author>Ed Tittel</pub:author>
    </pub:book>
    <pub:book>
     <pub:title>Java Developer?Guide to E-Commerce
     with XML and JSP</pub:title>
     <pub:author>William Brogden</pub:author>
    </pub:book>
   </pub:publications>
  </body>
</html>


Why We Need Namespaces

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