namespace of XML

Source: Internet
Author: User
Tags html tags naming convention resource xml parser xmlns xsl

XML namespaces provide a way to avoid element naming conflicts.

--------------------------------------------------------------------------------

Naming conflicts
Because the elements used in an XML document are not fixed, it can happen that two different XML documents use the same name to describe different types of elements. This often leads to naming conflicts. Take a look at the following two examples

This XML document carries information about the fruit in the table element:

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

This XML document carries information about the table in the table element (furniture, Can't eat OH):

<table>
<name>african Coffee table</name>
<width>80</width>
<length>120</length>
</table>

If the above two XML document fragments happen to be used together, then a naming conflict will occur. Since both fragments contain <table> elements, the definitions of these two table elements are different from those contained in the contents.

--------------------------------------------------------------------------------

Using prefixes to resolve naming conflict issues
The following XML document carries information in the TABLE element:

The following XML document carries information about the furniture table:

<f:table>
<f:name>african Coffee table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

There is now no problem with element naming conflicts, because the two documents use different prefixes for their table elements, and the table element is (

By using the prefix, we created two different table elements.

--------------------------------------------------------------------------------

Using namespaces
The following XML document carries information in the TABLE element:

The following XML document carries information about the furniture table:

<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>

In the above two examples, in addition to using prefixes, the two table elements use the xmlns attribute, associating the elements with different namespaces.

--------------------------------------------------------------------------------

Namespace Properties
Namespace attributes are generally placed at the beginning of an element, and the syntax is shown as follows:

Xmlns:namespace-prefix= "Namespace"

In the example above, the namespace defines an Internet address:

xmlns:f= "Http://www.w3schools.com/furniture"

The universal naming convention declares that the namespace itself is a uniform resource identifier, uniform Resource Identifier (URI).

When we use namespaces at the start tag of an element, all child elements of that element will be associated with the same namespace through a prefix.

Note: The network address used to identify the namespace is not invoked by the XML parser, and the XML parser does not need to look up information from this network address, which is simply a unique name for the namespace, so this network address can also be virtual, However, many companies often put this network address value like a real Web page, which contains more detailed information about the current namespace. can access http://www.w3.org/TR/html4/.

--------------------------------------------------------------------------------

Uniform Resource Identifier
A generic resource identifier (a uniform Resource Identifier (URI)) is a string that identifies a network resource. The most common URI should be the Uniform Resource Locator uniform Resource Locator (URL). The URL is used to identify the address of the network host. On the other hand, another infrequently used URI is the generic resource name Universal Resource name (URN). In our example, URLs are generally used.

Since the previous example uses the URL address to identify the namespace, we can be sure that the namespace is unique.

--------------------------------------------------------------------------------

The default namespace
Defining a default XML namespace allows us to not use prefixes in the start tag of child elements. His syntax is as follows:

<element xmlns= "Namespace" >

The following XML document contains information about the fruit in the table element:

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

The following XML document contains information about the furniture table:

<table xmlns= "Http://www.w3schools.com/furniture" >
<name>african Coffee table</name>
<width>80</width>
<length>120</length>
</table>

--------------------------------------------------------------------------------

Using namespaces
When you start using XSL, you'll find that namespaces are used so often. An XSL style sheet is used primarily to convert an XML document into a format similar to an HTML file.

If you look at the following XSL document, you'll find that there are many tags that are HTML tags. Those tags are not HTML tags, are prefixed xsl, and this XSL prefix is identified by the namespace "http://www.w3.org/TR/xsl":

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/xsl" >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.