Namespaces are a way to associate a group of elements with each other.
<wire:order xmlns:wire="http://www.wire-man.com/ns/1"> <!-->xmlns:localname="URI(realname)"<-->
<wire:store>
<wire:shirt xmlns:wire="http:/www.wire-man.com/ns/shirts"> <!-->覆盖realname,不能覆盖localname<-->
<wire:color>purple</wire:color>
</wire:shirt>
</wire:store>
</wire:order>
多个namespaces
<wire:order xmlns:wire="http://www.wire-man.com/ns/1"> <!-->1个<-->
<wire:store>
<wire:shirt xmlns:stitch="http:/www.wire-man.com/ns/stitch"> <!-->2个<-->
<wire:color>
<stitch:shade>lavender</stitch:shade>
</wire:color>
</wire:shirt>
</wire:store>
</wire:order>
Namespaces and attributes
元素前面加prefix,参数前面不用加
Namespaces and DTDs
DTD不支持Namespaces,只是当作一个attribute.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wire:order [
<!ELEMENT wire:order (wire:store)> <!-->元素前加上prefix<-->
<!ATTLIST wire:order xmlns CDATA #FIXED "http://www.wire-man.com/ns/1"> <!-->作为一个fixed参数处理<-->
<!ELEMENT wire:store (#PCDATA)>
]>
<wire:order xmlns="http://www.wire-man.com/ns/1">
<wire:store>Stitch Store</wire:store>
</wire:order>
Namespaces are not technically part of the XML 1.0 spec, thus,namespaces have nothing to do with the validity of an XML document.
Namespace and XML Schema
<!-->XML FILE<-->
<?xml version="1.0" encoding="UTF-8" ?>
<pants:order
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="namespace2.xsd"
xmlns:pants="http://www.wire-man.com/ns/1"> <!-->仅仅包括全局元素<-->
<store>30 paisley capri</store> <!-->这个元素不在namespaces中<-->
</pants:order>
<!-->XML Schema FILE namespace2.xsd<-->
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema <!-->定义一个namespace所有内容都饱含再一个xsd:schema元素内<-->
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" <!-->定义一个文档作为Schema<-->
targetNamespace="http://www.wire-man.com/ns/1" <!-->定义一个Namespace URI name<-->
xmlns:wire="http://www.wire-man.com/ns/1"> <!-->定义一个Namespace在schema中,localname不要求是一样得<-->
<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="store" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
××上面例子中注意xmlns:pants,targetNamespace, xmlns:wire是使用一样得URI值。
Assign the namespaces to all elements is elementFormDefault="qualified"
...
<xsd:schema
xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"
targetNamespace="http://www.wire-man.com/ns/1"
xmlns:wire="http://www.wire-man.com/ns/1"
elementFormDefault="qualified">
...
Assign the namespaces to the individule element
...
<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="store" type="xsd:string" form="qualified"/> <!-->***<-->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...
Exclude an element from the namespace
...
<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="store" type="xsd:string" form="unqualified"/> <!-->***<-->
</xsd:sequence>
</xsd:complexType>
</xsd:element>
...