views:

66

answers:

3

Hi all,
I am a bit novice in xml schema. I would be grateful if somebody help me out to understand why my xml is not being validated with the schema:

Here is my Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/testSchema" xmlns="http://www.example.org/testSchema"&gt;
  <xs:element name="Employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Name">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="FirstName" />
              <xs:element name="LastName" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Here is my test xml:

<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns="http://www.example.org/testSchema"&gt;
 <Name>
  <FirstName>John</FirstName>
  <LastName>Smith</LastName>
 </Name>
</Employee>

I am getting following error by Eclipse xml editor/validator:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'Name'. One of '{Name}' is expected.

I could not understand what is wrong with this schema or my xml.

A: 

Looks like you're failing to specify how to validate the FirstName and LastName elements; give the element specs for those type="xsd:string" (where xsd needs to be mapped to the XML Schema Datatypes namespace, of course) and all should be well.

But you are better off not nesting those elements so deep. Put them all at the same level and use ref="Name" to link them all together instead; it makes your schema much more flexible and usable.

Donal Fellows
No, I am failing on "Name" tag, not on First or Last name. This is why (perhaps) type didn't work. My "Name" element is of complex type so I cannot set its type. BTW, this example is simplified version of my relatively complex schema. By not using nested elements, my schema would make my life more difficult
Leslie Norman
+1  A: 

Just add the elementFormDefault="qualified" to the schema attribues.

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"         
       targetNamespace="http://www.example.org/testSchema"
       elementFormDefault="qualified"
       xmlns="http://www.example.org/testSchema"&gt;

And your original will work

 <?xml version="1.0" encoding="utf-8"?>
   <Employee xmlns="http://www.example.org/testSchema"&gt;
     <Name>
      <FirstName>John</FirstName>
      <LastName>Smith</LastName>
   </Name>
 </Employee>
Nix
By adding xmlns="" I got this error in xml: cvc-complex-type.2.4.a: Invalid content was found starting with element 'FirstName'. One of '{"http://www.example.org/ testSchema":FirstName}' is expected.
Leslie Norman
+1  A: 

all u have to do is add elementFormDefault="qualified" and u will be fine. to understand this behavior, read "Are You Qualified?" section @ http://msdn.microsoft.com/en-us/library/ms950796.aspx

Pangea
THANK YOU Pangea. This is the solution of my problem.
Leslie Norman