Favor Soup | 反胃书屋

斯是陋室,惟吾德馨。
posts - 34, comments - 18, trackbacks - 0, articles - 0
  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

XML Beginner notes (1)

Posted on 2007-04-04 16:20 D主 阅读(139) 评论(0)  编辑 收藏 引用 所属分类: XML相关
XML was designed to describe data and to focus on what data is while HTML was designed to display data and to focus on how data looks.
XML was designed to describe data, its tags are not predefined. You must define your own tags. XML uses a Document Type Definition (DTD) or an XML Schema to describe the data.
Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store and to send information.

XML Namespaces provide a method to avoid element name conflicts.
Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names.
EXAMPLES:
This XML document carries information in a table:
<table>
   <tr>
       <td>Apples</td>
       <td>Bananas</td>
   </tr>
</table>
This XML document carries information about a table (a piece of furniture):
<table>
    <name>African Coffee Table</name> 
    <width>80</width> 
    <length>120</length>
</table>
If these two XML documents were added together, there would be an element name conflict because both documents contain an element with different content and definition.

Solving Name Conflicts Using a Prefix

This XML document carries information in a table:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>

</h:tr>
</h:table>
This XML document carries information about a piece of furniture:
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>

<f:length>120</f:length>
</f:table>
Now there will be no name conflict because the two documents use a different name for their <table > element ( <h:table > and < f:table >). By using a prefix, we have created two different types of < table > elements.

Using Namespaces

This XML document carries information in a table:

<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>

<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

This XML document carries information about a piece of furniture:

<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>

</f:table>

Instead of using only prefixes, we have added an xmlns attribute to the <table> tag to give the prefix a qualified name associated with a namespace.

The XML Namespace (xmlns) Attribute

The XML namespace attribute is placed in the start tag of an element and has the following syntax:

xmlns:namespace-prefix="namespaceURI"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.

Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to a real Web page containing information about the namespace.

 

Uniform Resource Identifier (URI)

A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.
The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN). In our examples we will only use URLs.

Default Namespaces

Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax:

xmlns="namespaceURI"

This XML document carries information in a table:

<table xmlns="http://www.w3.org/TR/html4/">

<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>

This XML document carries information about a piece of furniture:

<table xmlns="http://www.w3schools.com/furniture">
<name>African Coffee Table</name>
<width>80</width>

<length>120</length>
</table>

只有注册用户登录后才能发表评论。