Detailed introduction to namespace code cases in XML

Source: Internet
Author: User
Tags xsl
The XML namespace provides a way to avoid element naming conflicts. XML # wiki/220.html "target =" _ blank "> 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:

 
 
Apples Bananas

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

 
  African Coffee Table
 
 
  80
 
 
  120
 
 
 

If the preceding two XML document fragments happen to be used together, a name conflict may occur. Because both parts contain

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

Use a prefix to solve naming conflicts
The following XML document contains information in the table element:

 
  
   
    Apples
   
   
    Bananas
   
  
 

The following XML document carries the furniture table information:

 
  
   African Coffee Table
  
  
   80
  
  
   120
  
 

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

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

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

Use namespace
The following XML document contains information in the table element:

 
  
   
    Apples
   
   
    Bananas
   
  
 

The following XML document carries the furniture table information:

 
  
   African Coffee Table
  
  
   80
  
  
   120
  
 

In the preceding two examples, besides the prefix, both table elements use the xmlns attribute to associate the 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"

The W3C naming convention declares that the namespace itself 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.

Uniform Resource Identifier
A Uniform Resource Identifier (URI) is A string that identifies network resources. The most common URI should be the 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 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:

 

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

 

  And the definition of these two table elements is different from that of the contained content. 
Apples Bananas

The following XML document contains the furniture table information:

 
  African Coffee Table
 
 
  80
 
 
  120
 
 
 

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

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:

 
 
  
  
 
Title Artist

The above is a detailed description of the namespace code case in XML. For more information, see other related articles in the first PHP community!

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.