Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do XML namespaces work without a working network connection?

<?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?

like image 992
ziggy Avatar asked Jan 14 '12 15:01

ziggy


People also ask

How does XML namespace work?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.

How are XML namespaces implemented?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is the purpose of using a default namespace in an XML file?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.

Why are XML namespaces urls?

Just like namespaces in . NET, namespaces in XML documents are designed to help prevent and resolve name collisions, especially if XML documents are from different organizations and/or domains of knowledge.


1 Answers

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.

like image 93
Michael Kay Avatar answered Oct 21 '22 20:10

Michael Kay