1
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

I've been reading up on XML for the past few days, and from what I understand about namespaces, namespaces are in a URI-format in order to differentiate between different elements of the same name, in-case authors want to work with same element names but used in different ways.

Apparently, no information is derived from the namespace, it's just an identifier (not sure about this). Information supposedly comes from the schemaLocation prefix, which provides a hint as to which XSD file to validate the document with (since "http://www.springframework.org/schema/beans" is the default namespace, I think it validates all elements without a prefix?). Correct me if I'm wrong.

I was messing around with the code above, and when I switched the xmlns="http://www.springframework.org/schema/beans" and xsi:schemaLocation="http://www.springframework.org/schema/beans

to another completely different URI, all of my elements get invalidated. I thought since I was associating a schema to a different namespace, my elements would still be validated correctly because the schema was the same. Is it possible that pre-defined elements do not come from the schema but from the namespace URI?

Also, how does the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace have an attribute called schemaLocation if it's just an identifier?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Slmy
  • 43
  • 4

1 Answers1

0

I've been reading up on XML for the past few days, and from what I understand about namespaces, namespaces are in a URI-format in order to differentiate between different elements of the same name, in-case authors want to work with same element names but used in different ways.

Yes, see also Why are XML namespaces HTTP addresses?

Apparently, no information is derived from the namespace, it's just an identifier (not sure about this). Information supposedly comes from the schemaLocation prefix, which provides a hint as to which XSD file to validate the document with (since "http://www.springframework.org/schema/beans" is the default namespace, I think it validates all elements without a prefix?). Correct me if I'm wrong.

While a namespace provides no intrinsic information, dependencies still exist against its value: The target namespace of the XSD must match it, and application software may exist that expects it.

Namespace prefixes are arbitrary abbreviations for namespace URIs; the namespace URI to which a prefix is bound is what matters.

I thought since I was associating a schema to a different namespace, my elements would still be validated correctly because the schema was the same.

You likely neglected to change the target namespace of the associated XSD. Also note that while perhaps useful as an exercise, it's not generally appropriate to change XML namespace URIs associated with published XSDs which you do not control.

Also, how does the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace have an attribute called schemaLocation if it's just an identifier?

See xmlns, xmlns:xsi, xsi:schemaLocation, and targetNamespace?

kjhughes
  • 106,133
  • 27
  • 181
  • 240