The meaning of XMLns in XML

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

Maven is a build tool that is difficult for Java developers to bypass, because many open source projects use Maven as their build tool. And reading the pom file in maven is an important part of understanding a project’s dependencies and construction methods. However, the head of the pom file will confuse beginners. Here is a clear and easy-to-understand explanation (combined from network information)

prerequisite knowledge

  • Understand the meaning of tags in xml and the tree-like nesting structure between tags
  • Understand pom (project object model) files

POM file header example

  1. < pre class="brush:java;gutter:true;">

< /li>

Beginners who see the above content should have the following points of confusion:

  • xmlns:xsi, xmlns, xsi:schmeLocation What is the meaning of these similar attributes, and why their values ​​are different URLs

  • Do these URLs correspond to some accessible resources, and whether the analysis of the file will require downloading the resources corresponding to these URLs

From the role of xmlns

If an xml document contains the following two elements with different definitions but the same name, the xml parser is It cannot be parsed because it cannot determine which element should be returned when you call document.getElementsByTagName("book").

  1.  
    
    Apples Bananas
    African Coffee Table80120
      

Obviously, if you add a prefix to their names, then The problem of naming conflicts can be resolved.

  1.  
     
     
    
     
    Apples
     
    Bananas
     
    
     
    
     
    
     
     
     
    African Coffee Table
     
    80
     
    120
     
    

However, in a document with many elements, only prefixes can’t Completely avoid the problem of naming conflicts.

  • Note that xml documents can be mutually included or referenced through XInclude and External Entites.

At this time, the namespace is born. We can define a namespace for the element and associate a very long string that can guarantee global uniqueness with the element. In this way, naming conflicts can be avoided.

But how to ensure that the longer string is globally unique? The best way is to use the Uniform Resource Identifier (URI), and our most common URI is usually accessed frequently. The URL of the website is up.

xmlns:namespace-prefix="namespaceURI"

Apply to us The example cited is:

  1.  
    
     
    
     
    Apples
     
    Bananas
     
    
     
    
     
    
     
    
     
    African Coffee Table
     
    80
     
    120
     
    
    

      

  • So, for the identifier of the namespace, the role of URI is only Is to ensure uniqueness, it does not need to correspond to an accessible resource or file! However, many companies will make the namespace URI point to a web page containing the namespace information

Go back to our POM document header, you will find http://www.w3.org/2001/XMLSchema-instance in project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" can access a normal page , But if you visit http://maven.apache.org/POM/4.0 in xmlns="http://maven.apache.org/POM/4.0.0" .0 will get a PAGE NOT FOUND error, the page is not retrieved.

The meaning of xmlns appearing in elements without a prefix

  1. < pre class="brush:java;gutter:true;">

      

Careful children’s shoes will notice that there is no prefix in front of the project element. Here is actually the default naming space (default naming space). Its syntax is as follows:

For example, our project element definition and the following example

Apples Bananas

  

The effect of using the default namespace is that the child elements inside the element will belong to the namespace by default, and we don’t need to add namespaces for them one by one Prefix.

xmlns:xsi and xsi:schemaLocation

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

  1. < pre class="brush:java;gutter:true;">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 our previous knowledge, we can understand that xmlns:xsi defines a namespace prefix xsi The corresponding unique string http://www.w3.org/2001/XMLSchema-instance. However, readers will find that this xmlns:xsi seems to appear in different xml documents. This is because xsi has become the industry’s default namespace for XSD ((XML Schema Definition) files. And XSD files (also often called Schema files) are used to define the structure of xml documents.

  • Note: The XML parser can parse another XML file according to the content of an XSD file, and determine whether the structure of the file is consistent with the definition in the XSD file. XSD files can be understood as the grammar or format of the XML document that can be customized Checker.

So, with the above understanding, let’s look again

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

< p>The syntax of the above line is actually, xsi:schemaLocation = "key" "value"
That is, the value of the schemaLocation element in the xsi namespace is separated by a space Key-value pair.

  • The previous “key” http://maven.apache.org/POM/4.0.0 refers to [namespace], which is just a globally unique string That’s it

  • The next value refers to [XSD location URI], this value indicates the location of the XSD file corresponding to the previous namespace, xml parser can use this information to get XSD file, so that
    all the element structures belonging to the namespace http://maven.apache.org/POM/4.0.0 are verified through the XSD file, so this value must be accessible and accessible The content is the content of an XSD file

  • < /ul>

Maven is a build tool that is difficult for Java developers to bypass, because there are many open source projects Use Maven as its build tool. And reading the pom file in maven is an important part of understanding a project’s dependencies and construction methods. However, the head of the pom file will confuse beginners. Here is a clear and easy-to-understand explanation (combined from network information)

prerequisite knowledge

  • Understand the meaning of tags in xml and the tree-like nesting structure between tags
  • Understand pom (project object model) files

POM file header example

  1. < pre class="brush:java;gutter:true;">