MaxNevermind
MaxNevermind

Reputation: 2933

Default namespace in XML file doesn't work with XSD, why?

I have a XML file and XSD for it. In this form it works fine:

<tns:Users xmlns:tns="http://www.example.org/NewXMLSchema">
    <User>
        <FirstName>Max</FirstName>
        <LastName>Gordon</LastName>
        <Salary>80000</Salary>
    </User>
    <User>
        <FirstName>Alex</FirstName>
        <LastName>Disel</LastName>
        <Salary>75000</Salary>
    </User>
</tns:Users>

<schema xmlns="http://www.w3.org/2001/XMLSchema"  
        targetNamespace="http://www.example.org/NewXMLSchema" 
        xmlns:tns="http://www.example.org/NewXMLSchema">
  <element name="Users">
    <complexType>
      <sequence maxOccurs="unbounded" minOccurs="1">
        <element name="User">
          <complexType>
            <sequence>
              <element name="FirstName" type="string"/>
              <element name="LastName" type="string"/>
              <element name="Salary" type="int"/>
            </sequence>
          </complexType>
        </element>
      </sequence>
    </complexType>
  </element>
</schema>

I wonder why it doesn't in another: if I omitted tns prefixes in xml file? I mean it would became a default namespace then:

<Users xmlns="http://www.example.org/NewXMLSchema">
    <User>
        <FirstName>Max</FirstName>
        <LastName>Gordon</LastName>
        <Salary>80000</Salary>
    </User>
    <User>
        <FirstName>Alex</FirstName>
        <LastName>Disel</LastName>
        <Salary>75000</Salary>
    </User>
</Users>

Upvotes: 0

Views: 413

Answers (1)

ColdFusion
ColdFusion

Reputation: 2531

Because these are different XML documents.

In the first XML:

<tns:Users xmlns:tns="http://www.example.org/NewXMLSchema">
    <User>
        <FirstName>Max</FirstName>
        <LastName>Gordon</LastName>
        <Salary>80000</Salary>
    </User>
    <User>
        <FirstName>Alex</FirstName>
        <LastName>Disel</LastName>
        <Salary>75000</Salary>
    </User>
</tns:Users>

only the root element Users is in http://www.example.org/NewXMLSchema namespace. All other elements are in {no namespace}.

This corresponds to your XML schema. It does define the target namespace. But it applies only to global element Users. All other elements are declared locally, and their namespace is determined by the elementFormDefault attribute of the <schema ...> element. You don't specify this attribute, but it exists and its default value is "unqualified". That means that all local elements have no namespace.

Now, let's look in your second XML:

<Users xmlns="http://www.example.org/NewXMLSchema">
    <User>
        <FirstName>Max</FirstName>
        <LastName>Gordon</LastName>
        <Salary>80000</Salary>
    </User>
    <User>
        <FirstName>Alex</FirstName>
        <LastName>Disel</LastName>
        <Salary>75000</Salary>
   </User>
</Users>

Here, you bluntly specify that all elements are in http://www.example.org/NewXMLSchema namespace (both the root and everything else). But this doesn't comply with your XML schema!

Upvotes: 2

Related Questions