what is the exact usage of xmlns in xml, and html

前端 未结 5 2449
-上瘾入骨i
-上瘾入骨i 2021-02-20 02:42

Does any one know what is the exact usage of xmlns in HTML, XML files?

Edit: Why do we need this namespace? What is its usage?

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-20 03:22

    The xmlns attribute has special handling, that allows the declaration of a namespace.

    All names, such as tag names, in a document belong to a namespace. In the absence of the xmlns attribute all the names belong to the "no name" namespace. Hence:-

    
    

    In the above example both root and item are names in the "no name" namespace. Whereas:-

    
    

    Now root and item exist in the "urn:mydomain.com:mystuff" namespace.

    The xmlns can further define additional namespaces elements of which can be distinguished from others by using an alias prefix:-

    
        
           
         
    
    

    In this case root and item continue to be in the "urn:mydomain.com:mystuff" namespace but a:supplement indicates that the name supplement is in the "urn:otherdomain.com:other" namespace.

    What does this acheive?

    The X in XML stands for eXtensible. One goal is to allow additional information to layer onto an existing document, i.e., the ability to extend the document. Consider:-

    Party A create a document:-

     
        
     
    

    Party B extends the document by including additional information:-

     
        
        
     
    

    Later Party A adds new info to their original form of the document and just so happen to also use the name supplement in their original. We could end up with something like:-

     
        
        
        
     
    

    Which supplement element belongs to which party? By using namespaces the document would look like this:-

     
        
        
        
     
    

    Now when it comes to parsing and querying the XML its clear which element belongs to whom. Namespaces elimnate the collision between what would otherwise be a global set of simple names.

提交回复
热议问题