How do XML namespaces work without a working network connection?

后端 未结 4 1335
南笙
南笙 2020-12-05 14:28




        
相关标签:
4条回答
  • 2020-12-05 15:05

    Let us assume we have this XML document.

    <?xml version="1.0" encoding="UTF-8"?>
    <html>
          <body>
            Your text here
          </body>
          <body>
            <height>182 cm</height>
            <weight>83 kg</weight>
          </body>
    </html>
    

    It includes HTML which has a body tag with a semantic meaning for a HTML renderer. It also has another body tag which carries information about a specific person. A namespace defines a semantic scope for this tag. Without a namespace(as in the example provided), it is impossible for a parser to tell the difference because they are syntactically the same.

    Here is the semantically correct version of the same document:

    <?xml version="1.0" encoding="UTF-8"?>
    <html:html xmlns:html="http://www.w3.org/TR/xhtml1/">
      <html:body>
        Your text here
      </html:body>
      <human:body xmlns:human="http://www.example.com/human/">
        <human:height>182 cm</human:height>
        <human:weight>83 kg</human:weight>
      </human:body>
    </html:html>
    

    Thus thanks to namespaces we do not have worry about conflicting tags with different meanings.

    The namespace URIs themselves are never actually resolved, and are arbitrary (thus you can use them offline).

    0 讨论(0)
  • 2020-12-05 15:11

    Try to ignore the fact that many namespace names look like URLs that you might type into your browser. They are just random strings of characters, they aren't addresses of resources on the web. The reason people adopt this convention is that it shows who "owns" the name - it's clearer what http://www.w3.org/2001/XMLSchema refers to than if they had chosen "xsd1.0" as the namespace name, and it's less likely to conflict accidentally with a name chosen by someone else. Some people also like the fact that you can put documentation at the relevant location, but no XML software will go looking for the documentation automatically.

    0 讨论(0)
  • 2020-12-05 15:11

    An XML namespace is a component of an element's name, which you can bind to a prefix when you write xmlns:<prefix>="<namespace>". That helps to avoid naming conflicts between different XML schemas, so that you can mix elements from two schemas that happen to have the same name. For example, you might have two schemas that both have link elements with different meanings, and the namespace prefix lets you distinguish between them by writing either foo:link or bar:link. Namespaces are typically in the form of a URL, but the parser just treats it as a string, it doesn't try to fetch anything from that URL.

    You're correct about the second part, the XSI:SchemaLocation element. See this answer for why it's still able to validate the schema without a network connection.

    0 讨论(0)
  • 2020-12-05 15:12

    Namespace in general is to avoid conflict between the tags, but behind the scene it is a processing instruction for a parser against some schema. In browser you have for example different Document type, strict Xhtml and so on, the browser has those schemas somewhere stored and the parser refers to those schema for validating the structure.

    In Spring once you enable a particular schema, it needs a reference where that schema exists, it does not mean the URI schema has no meaning. The related .jar files will be needed in class-path, so that parser refers to it. org.springframework.context-4.1.jar. If you look into this jar file you will find in package org.springframework.context.config that spring-context-2.5.xsd file present. You define: xmlns:context="http://www.springframework.org/schema/context"

    and

    xsi:schemaLocation="http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    

    The xmlns gives you through your editor to use preconfigured means to write for example <context tag that comes from xmlns:context, and enables validated TAG like:

     <context:annotation-config> 
    

    Which in TURN activates various annotations to be detected in bean classes: @Required @Autowired, @PostConstruct, @PreDestroy and @Resource and like.

    So in effect the parser will find meaning while parsing the Beans.

    The schema is very much needed and at least ONCE has to be downloaded that your IDE or application will use for validation, otherwise it will create validation exception. If it is locally available within those jars then it won't look for download.

    0 讨论(0)
提交回复
热议问题