Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" a special case in XML?

When we use a namespace, we should also indicate where its associated XSD is located at, as can be seen in the following example:

<?xml version="1.0"?>
<Artist BirthYear="1958" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.webucator.com/Artist"
 xsi:schemaLocation="http://www.webucator.com/Artist Artist.xsd">
 <Name>
  <Title>Mr.</Title>
  <FirstName>Michael</FirstName>
  <LastName>Jackson</LastName>
 </Name>
</Artist>

Here, we have indicated that Artist.xsd should be used for validating the http://www.webucator.com/Artist namespace. However, we are also using the http://www.w3.org/2001/XMLSchema-instance namespace, but we have not specified where its XSD is located at. How do XML parsers know how to handle this namespace?

Update (in response to the first commenter)

So, can we instead of using:

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

use

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:ehcache="http://www.springmodules.org/schema/ehcache">
...
</beans>

?

like image 950
Behrang Avatar asked Apr 11 '10 02:04

Behrang


People also ask

What does xmlns XSI http www w3.org 2001 XML Schema instance mean?

xmlns:xsi:http://www.w3.org/2001/XMLSchema-instance — Declared in the XML and is used to help declare that the XML document is an instance of and XSD. The convention is to use the namespace prefix of xsi. targetNamespace: is used in the header of the XSD to defined the namespace that the XSD describes.

What does XSI mean in XML?

The prefix "xsi" is the namespace prefix used by convention for the XML Schema instance namespace. XML documents can contain elements that have an xsi:type attribute. This behavior provides an explicit data type for the element. The MRM XML parser in sensitive to xsi:type attributes in the XML document.

What is XML Schema instance?

The schema instance namespace (http://www.w3.org/2001/XMLSchema-instance) defines a few attributes that are used in instance documents with special meaning. The xsi:noNemaspaceSchemaLocation and xsi:schemaLocation attributes are used to associate XML Schemas with XML documents.

What is xmlns in XML Schema?

In the attribute xmlns:pfx, xmlns is like a reserved word, which is used only to declare a namespace. In other words, xmlns is used for binding namespaces, and is not itself bound to any namespace. Therefore, the above example is read as binding the prefix "pfx" with the namespace "http://www.foo.com."


1 Answers

How do XML parsers know how to handle this namespace?

They don't, except when they do. The basic idea is that the string 'http://www.w3.org/2001/XMLSchema-instance' works like a magic cookie. Either the processing software has been programmed to recognize it, and thus act on the basis of what it means, or it has not.

Thus, with the mere fact of recognition also comes the "knowledge" of what it represents: a "namespace" that defines four attributes ('type', 'nil', 'schemaLocation' and 'noNamespaceSchemaLocation') with fixed predefined meanings.

In other words, if you "know" what the string 'http://www.w3.org/2001/XMLSchema-instance' "means", then you also automatically know what an attribute named xsi:schemaLocation "means": that it points to schema documents encoded in the 'W3C XML Schema' formalism.

This goes beyond what the XML Namespaces Rec actually provides for (which is only some handwaving about "universal names" or whatnot). A convention is at work here, that the syntax of namespacing (using colonified names) has been deployed to hard-code a semantic understanding: "where to find the schema, in the formalism of W3C XML Schemas, for this document instance." It all hinges on prior understanding of that magic cookie string.

You may be under the impression that a namespace must have a schema, and a machine processable one at that, and only in the W3C XML Schemas formalism to boot. None of these are necessarily true. Other schema formalisms exist (SGML/XML DTDs, Relax-NG, both of which, unlike W3C XML Schemas, are international standards); a namespace definition does not have to be machine-readable (it could be prose, as in fact it is for the 'http://www.w3.org/2001/XMLSchema-instance' namespace!); and a namespace need not be formally defined at all, because all a namespace string is guaranteed to do is to function as a disambiguation marker.

like image 176
arayq2 Avatar answered Nov 16 '22 00:11

arayq2