21
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.1.xsd">

  <context:component-scan
      base-package="com.springinaction.chapter01.knight" />

</beans>

The above example shows an example of an XML file with several namespaces. What is the purpose of these namespaces and most importantly why do they work even where there is no Internet connection?

I think the second bit that starts with xsi:schemaLocation contains the XML schema files which are used to validate the structure of the XML document. How come these still work if I run the application that uses this configuration file on a machine that is not on a network? Are the URLs somehow aliases to JAR files?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
ziggy
  • 15,677
  • 67
  • 194
  • 287

4 Answers4

20

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • nice one thank you. i have a doubt when i use beans namepace in java if i miss bean class tag, ecllipse will show an error like you missed that. How will that namespace works without resolving. pardon me my English is not good. – Raghavendra Oct 24 '14 at 07:08
18

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).

MatRt
  • 3,494
  • 1
  • 19
  • 14
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
4

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.

Community
  • 1
  • 1
Tony
  • 3,470
  • 1
  • 18
  • 23
  • An XML namespace is *not* a prefix and describing it so can only cause confusion. The namespace is that string that typically looks like a URL. A prefix *may* be used to **bind** the element to its namespace. – Alohci Jan 14 '12 at 20:25
  • @Alohci Thanks for pointing that out, I didn't mean to imply the prefix was the namespace. I've edited my answer to distinguish the two. – Tony Jan 15 '12 at 14:11
0

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.

vimal krishna
  • 2,886
  • 28
  • 22