what is the use of xsi:schemaLocation?
Asked Answered
I

4

143

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?

Intermittent answered 3/5, 2011 at 20:3 Comment(0)
B
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.

Bialy answered 3/5, 2011 at 20:37 Comment(8)
Wouldn't the XML parser search the class path?Rainie
@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?Triceratops
@HDave But what would XML parser be looking for, schema could have any nameKumiss
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.Septimal
For more info on the spring intercept layer, see https://mcmap.net/q/109899/-spring-di-applicationcontext-xml-how-exactly-is-xsi-schemalocation-usedCellist
@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 payloadAnzovin
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?Tunny
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.Hollander
S
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.

Spikenard answered 3/5, 2011 at 20:6 Comment(0)
C
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://mcmap.net/q/107495/-how-to-validate-an-xml-file-against-an-xsd-file for slightly more info.

Cellist answered 22/1, 2015 at 20:47 Comment(0)
H
-1

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.

Highroad answered 1/11, 2017 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.