XML(6) Namespace

In this example, <title> tag can be ambiguous. It can mean a person's name for the name from the résumé

<?xml version="1.0"?>
<person>
     <name>
          <title>Sir</title>
          <first>John</first>
          <middle>Fitzgerald Johansen</middle>
          <last>Doe</last>
     </name>
     <position>Vice President of Marketing</position>
     <résumé>
          <html>
          <head><title>Resume of John Doe</title></head>
          <body>
              <h1>John Doe</h1>
              <p>John's a great guy, you know?</p>
          </body>
          </html>
          </résumé>
</person>


Namespace

To use XML namespaces, elements are given qualified names.
Qualified name is abbreviated to QName.
These QName 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.

When namespace is applied to the xml document:

<?xml version="1.0"?>
<pers:person xmlns:pers="http://www.wiley.com/pers"
                                      xmlns:html="http://www.w3.org/1999/xhtml">
     <pers:name>
       <pers:title>Sir</pers:title>
       <pers:first>John</pers:first>
       <pers:middle>Fitzgerald Johansen</pers:middle>
       <pers:last>Doe</pers:last>
     </pers:name>
     <pers:position>Vice President of Marketing</pers:position>
     <pers:resume>
     <html:html>
       <html:head><html:title>Resume of John Doe</html:title></html:head>
       <html:body>
       <html:h1>John Doe</html:h1>
       <html:p>John's a great guy, you know?</html:p>
       </html:body>
     </html:html>
     </pers:resume>
</pers:person>

Notice you can distinguish which the titles with namespace now.

The pers is the default namespace


Default Namespace

A default namespace is like a regular namespace. You don't have to specify a prefix for all of the elements that use it.

In this example http://www.wiley.com/pers is the default namespace

The namespace will only apply to itself and its descendant. So "http://www.w3.org/1999/xhtml" will be the namespace for the html part of the person.xml

<?xml version="1.0"?>
<person xmlns="http://www.wiley.com/pers">
   <name>
     <title>Sir</title>
     <first>John</first>
     <middle>Fitzgerald Johansen</middle>
     <last>Doe</last>
   </name>
   <position>Vice President of Marketing</position>
   <résumé>
     <html xmlns="http://www.w3.org/1999/xhtml">
       <head><title>Resume of John Doe</title></head>
       <body>
       <h1>John Doe</h1>
       <p>John's a great guy, you know?</p>
       </body>
     </html>
   </résumé>
</person>

Namespace and attribute

<person xmlns="http://www.wiley.com/pers">
   <name id="25">
     <title>Sir</title>
   </name>
</person>

In the preceding example, although name is in the default namespace, id="25" is NOT in the same namespace as name

to make id="25" in the same namespace as name, explicitly specify it:

<a:person xmlns:a="http://www.wiley.com/pers">
   <a:name a:id="25">
     <a:title>Sir</a:title>
   </a:name>
</a:person>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值