0

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>

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

"But this doesn't comply with your XML schema!" Why? I pointed out targetNamespace="example.org/NewXMLSchema" so my XSD is targeted for the exact namespace where my xml file is.
Because XML works with full names. What you see in XML document are local names. But the full XML name is this: {namespaceURI}localName. So, in the first XML, you have, for instance: {}FirstName. But in the second XML, the same will be: {example.org/NewXMLSchema}FirstName. Got the difference? If not, please read some basic XML book. I recommend this: O'REILLY, "XML IN A NUTSHELL". targetNamespace in <schema> applies only to global components (basically those whose root is <schema>). Whether it applies to the locally declared elements is controlled by elementFormDefault attribute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.