142

I see that we have multiple url's as value of this attribute like in spring:

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

Why is it needed and what is it used for? Does Spring goes to the url and validate? what is the difference between xmlns and xsi:schemaLocation?

Oded
  • 489,969
  • 99
  • 883
  • 1,009
sab
  • 9,767
  • 13
  • 44
  • 51

4 Answers4

92

The Java XML parser that spring uses will read the schemaLocation values and try to load them from the internet, in order to validate the XML file. Spring, in turn, intercepts those load requests and serves up versions from inside its own JAR files.

If you omit the schemaLocation, then the XML parser won't know where to get the schema in order to validate the config.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Wouldn't the XML parser search the class path? – Dave Jan 26 '12 at 14:06
  • 1
    @skaffman, Does it mean that when I run maven build of spring-based projhect with -o (offline) flag the build will fail despite the fact that all the dependencies are available in my local repository? – aviad Oct 22 '12 at 07:40
  • @HDave But what would XML parser be looking for, schema could have any name – Krzysztof Krasoń Apr 05 '14 at 07:56
  • Technically, the XML parser will firstly try to load the schema from internet, if not found or internet access is not available, it will search *.xsd file locally from class path, if still not found, it will be omitted. – Frank Zhang Nov 16 '15 at 02:35
  • For more info on the spring intercept layer, see http://stackoverflow.com/a/10768972/32453 – rogerdpack Jan 09 '17 at 19:34
  • @skaffman, does this mean if there is no parser layer like in Spring, we do not need to provide the xsi:schemaLocation information ? like in a SOAP payload – sthbuilder Apr 13 '18 at 22:26
  • What is the purpose of declaring the the xsi namespace without its xsd location? Can the xsi namespace be omitted completely and the XML parser still understand the schemaLocation attribute? – jmrah Jan 16 '19 at 13:28
  • Parsers do not look for schemas on the internet. They look for any schemas registered *as* that URI. It's considered a dangerous configuration to permit that. – Dragas Mar 26 '22 at 17:23
74

An xmlns is a unique identifier within the document - it doesn't have to be a URI to the schema:

XML namespaces provide a simple method for qualifying element and attribute names used in Extensible Markup Language documents by associating them with namespaces identified by URI references.

xsi:schemaLocation is supposed to give a hint as to the actual schema location:

can be used in a document to provide hints as to the physical location of schema documents which may be used for assessment.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
29

According to the spec for locating Schemas

there may or may not be a schema retrievable via the namespace name... User community and/or consumer/provider agreements may establish circumstances in which [trying to retrieve an xsd from the namespace url] is a sensible default strategy

(thanks for being unambiguous, spec!)

and

in case a document author (human or not) created a document with a particular schema in view, and warrants that some or all of the document conforms to that schema, the schemaLocation and noNamespaceSchemaLocation [attributes] are provided.

So basically with specifying just a namespace, your XML "might" be attempted to be validated against an xsd at that location (even if it lacks a schemaLocation attribute), depending on your "community." If you specify a specific schemaLocation, then it basically is implying that the xml document "should" be conformant to said xsd, so "please validate it" (as I read it). My guess is that if you don't do a schemaLocation or noNamespaceSchemaLocation attribute it just "isn't validated" most of the time (based on the other answers, appears java does it this way).

Another wrinkle here is that typically, with xsd validation in java libraries [ex: spring config xml files], if your XML files specifies a particular schemaLocation xsd url in an XML file, like xsi:schemaLocation="http://somewhere http://somewhere/something.xsd" typically within one of your dependency jars it will contain a copy of that xsd file, in its resources section, and spring has a "mapping" capability saying to treat that xsd file as if it maps to the url http://somewhere/something.xsd (so you never end up going to web and downloading the file, it just exists locally). See also https://stackoverflow.com/a/41225329/32453 for slightly more info.

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
0

If you go into any of those locations, then you will find what is defined in those schema. For example, it tells you what is the data type of the ini-method key words value.

Mojun Zhu
  • 1
  • 1