XML guide-XML namespace

Source: Internet
Author: User
Tags xml parser xsl

The XML namespace provides a way to avoid element naming conflicts.

Name Conflict

Because the elements used in the XML document are not fixed, two different XML documents may use the same name to describe different types of elements. In this case, naming conflicts often occur. See the following two examples.

This XML document contains fruit information in the table element:

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

This XML document contains the table information (furniture, which cannot be eaten) in the table element ):

<table><name>African Coffee Table</name><width>80</width><length>120</length></table>

If the preceding two XML document fragments happen to be used together, a name conflict may occur. Because both fragments contain <Table> elements, the definitions of these two table elements are different from those of the contained content.

Use a prefix to solve naming conflicts

The following XML document contains information in the table element:

The following XML document carries the furniture table information:

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

Now there is no element naming conflict problem, because the two documents use different prefixes for their respective table elements. The table elements in the two documents are respectively (

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

Use namespace

The following XML document contains information in the table element:

The following XML document carries the furniture table information:

<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 preceding two examples, besides the prefix, both table elements useXmlnsAttribute to associate elements with different namespaces.

Namespace attributes

The namespace attributes are generally placed at the start of an element. The syntax is as follows:

xmlns:namespace-prefix="namespace"

In the preceding example, The namespace defines an Internet address:

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

W3C naming convention declares that a namespace is a unified resource identifier,Uniform Resource Identifier (URI).

When a namespace is used at the beginning of an element, all the child elements of the element are associated with the same namespace through a prefix.

Note: The network address used to identify the namespace is not called by the XML parser. The XML parser does not need to find information from this network address, the network address only serves to give the namespace a unique name, so the network address can also be virtual, however, many companies often regard this network address as a real web page, which contains more detailed information about the current namespace.
You can access the http://www.w3.org/TR/html4.

Uniform Resource Identifier

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

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

Default namespace

Define a default XML namespace so that we do not need to use the prefix in the start tag of the child element. Its syntax is as follows:

<element xmlns="namespace">

The following XML document contains fruit information 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 the furniture table information:

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

Use namespace

When the file starts to use XSL, it will find that the namespace is used so frequently. The XSL style sheet is mainly used to convert an XML document into a format similar to an HTML file.

If you look at the XSL document under idea, you will find that many tags are HTML tags. The markup is not an HTML markup, but an XSL with a prefix, which is identified by the namespace "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.