3
votes

I have few questions about xml namespace, I will explain with these three pieces of code:

1 - Very simple XML Schema:

<?xml version="1.0" encoding="US-ASCII"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:tns="http://www.library.com"
        targetNamespace="http://www.library.com"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">

<element name="Book" type="tns:BookType" />

<complexType name="BookType">
  <sequence>
    <element name="Title" type="string" />
    <element name="Author" type="string" />
  </sequence>
</complexType>

</schema>

2 - XML that use the newly created xml schema:

<?xml version="1.0" encoding="US-ASCII"?>
<Book xmlns:xsi="http://www.wc3.org/2001XMLSchema-instance"
            xsi:schemaLocation="http://www.library.com ex9.xsd"
            xmlns="http://www.library.com">

   <Title>Scherlock Holmes</Title>
   Author>Arthur Conan Doyle</Author>
</Book>

3 - Another fragment code without relationship from the two above:

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

Question are:

  1. Why always are we declare namespaces like xmlns="http://www.w3.org/2001/XMLSchema" and xmlns:xsi="http://www.wc3.org/2001/XMLSchema-instance" but no schemaLocation for these is given?
  2. How an XML parser will know (for example for to validate) that xmlns="http://www.w3.org/2001/XMLSchema" define elements like <attribute>, <complexType>, <sequence>, etc?
  3. Reading many posts I understood that namespaces and thus their URI, means basically nothing, they are used just for avoid name conflicting. But I read also that if you declare xmlns="http://www.w3.org/2001/XMLSchema" namespace wrong the XML file will not valid, why?
  4. Why In the last code fragment always no schemaLocation is given for http://www.w3.org/2001/XMLSchema-instance.
1

1 Answers

2
votes
  1. Those built-in namespaces pertain to XSD components themselves. No schemaLocation is necessary because their definition is implied by the XML Schema Recommendation.
  2. A conformant XML parser by definition will understand the meaning of xs:attribute, etc.
  3. I wouldn't say that namespaces mean nothing. Beyond being a way of differentiating otherwise identifically named components, namespaces can also be used to associate the use of components with their collective definition in another XSD.
  4. As stated in #1, http://www.w3.org/2001/XMLSchema-instance is a built-in namespace consisting of components whose definitions are already implied by the XML Schema Recommendation.