Meaning in XML xmlns attributes such as

Original: https: //blog.csdn.net/lengxiao1993/article/details/77914155

 

Maven is a java developer is difficult to bypass the build tool, because there are many open-source projects use Maven as their build tool. The maven pom file read in is an important part of understanding a project dependencies and build approach. But beginners will head confused pom file, where it carried out a clear and understandable explanation of (combing data from the network)

Prerequisite knowledge

  • The tree structure between the nested meaning as commonly understood in the xml tag and label
  • Learn pom (project object model) file

POM file header example

  1.  
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

Beginners should see the contents of the above points are as follows confusion:

  • xmlns:xsi ,  xmlnsxsi:schmeLocation Which looks similar to the properties of what is the meaning of why their value is not the same URL

  • The URL corresponds some resources that can be accessed, whether to parse the file will need to download the corresponding URL resource

Let's talk about the role of xmlns

If an xml document contains the following two definitions differ, but the same element names, xml parser is unresolved, because it can not determine when you call  document.getElementsByTagName("book") upon the elements which should be returned.

  1.  
    <! - table element described herein is a table -> 
     
    <table> 
     
    <TR> 
     
    <TD> Apples </ TD> 
     
    <TD> Bananas </ TD> 
     
    </ TR> 
     
    </ table> 
     
    <! - here is a description of the table element home tables -> 
     
    <table> 
     
    <name> African Coffee the table </ name> 
     
    <width> 80 </ width> 
     
    <length> 120 </ length> 
     
    </ table>  

 

Obviously, if they are given names to add a prefix, the naming conflict issues can be resolved.

  1.  
    <! - table element described herein is a table -> 
     
    <H: table> <- - Added prefix H!> 
     
    <H: TR> 
     
    <H: TD> Apples </ H: TD> 
     
    < H: TD> Bananas </ H: TD> 
     
    </ H: TR> 
     
    </ H: table> 
     
    <- here the table element is a table described ->! 
     
    <F:! table> <- added prefix F -> 
     
    <F: name> African Coffee the Table </ F: name> 
     
    <F: width> 80 </ F: width> 
     
    <F: length> 120 </ F: length> 
     
    </ F: Table>

However, in a document has many elements, only has a prefix, you can not completely avoid the problem of naming conflicts.

  • Note each other xml document is by XInclude,  External entites  achieve mutually inclusive or references.

At this point, the namespace was born, we can define a namespace for the element will be a very long, can ensure global uniqueness of the string associated with the element. This can be avoided naming conflict.

But how to ensure that a long string of globally unique it is the best way than using a uniform resource identifier (Uniform Resource Identifier, URI), while the web site of our most common URI is the URL I usually visit.

xmlns:namespace-prefix="namespaceURI"

Applied to our example of that:

  1.  
    <! - table element described herein is 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> 
     
    <- here the table element is a table described ->! 
     
    <F : Table xmlns: F = "http://www.w3school.com.cn/furniture"> 
     
    <F: name> African Coffee the Table </ F: name> 
     
    <F: width> 80 </ F: width> 
     
    <F : length> 120 </ F: length> 
     
    </ F: Table>
    

      

  • So, for the namespace identifier URI's role is to ensure that only unique, it does not require a corresponding resource or file can be accessed! However, there are a lot of companies will allow URI namespace of the namespace to point to a Web page containing information

Returning to our POM head of the document, you will find  project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" the http://www.w3.org/2001/XMLSchema-instance have access to a normal page, but if you visit  xmlns="http://maven.apache.org/POM/4.0.0" the  http: //maven.apache. org / POM / 4.0.0  'll get a pAGE nOT FOUND error, page not get to.

xmlns prefix appears there is no element of meaning

  1.  
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    

      

The shoes will be carefully noted that the previous project elements and no prefix. There are really using the default namespace (default naming space). Its syntax is as follows:

<elementName xmlns="namespaceURI">

For example, we define the elements of the project and the following examples

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

  

Use the default namespace is the role of sub-elements will default the interior of the elements belonging to that namespace, we do not need to add eleven namespace prefix for them.

xmlns:xsi 与 xsi:schemaLocation

Now look at the rest of the file header, looks more complicated part. 

  1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"

According to previous knowledge we can understand,  xmlns:xsi define a namespace prefix  xsi unique string corresponding  http://www.w3.org/2001/XMLSchema-instance. But the reader will find that this  xmlns:xsi will seem to appear in different xml document. This is because,  xsi has become an industry default for XSD ((namespace XML Schema Definition) file. The XSD file (often called Schema file) is used to define the xml document structure.

  • Annotations: XML parser to parse another XML file based on the contents of an XSD file, determines whether the file structure consistent and defined XSD file. XSD file can be understood as an XML document format or grammar checker that can be customized.

So, with the above understanding, and then look

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"

The syntax above this line is actually yes,  xsi:schemaLocation = "键" “值” 
that is the value schemaLocation xsi name space under the elements separated by a space pairs.

  • Before a "key"  http://maven.apache.org/POM/4.0.0  refer to [namespace], but only a globally unique string

  • After a value refers to [XSD location URI], this value indicates the position before a namespace corresponding XSD file, xml parser can use this information acquired XSD file, so that by XSD file of 
    all in namespace  http: / /maven.apache.org/POM/4.0.0  content elements verify structure, so this value must be accessible, and access to the contents of an XSD file

Guess you like

Origin www.cnblogs.com/wangcp-2014/p/11277605.html