I built the following ('Person.xsd') XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:person.com.test"
xmlns="urn:person.com.test">
<xs:element name="person" type="Person" />
<xs:complexType name="Person">
<xs:sequence>
<xs:element name="first_name" type="xs:string" />
<xs:element name="last_name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
And the following XML document ('Person.xml'):
<?xml version="1.0"?>
<person
xmlns="urn:person.com.test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:person.com.test person.xsd" >
<first_name>Joe</first_name>
<last_name>Bloggs</last_name>
</person>
But when I validate the XML (I'm using Netbeans 8.x , but other validators I have tried give very similar results); I get the following unhelpful message:
XML validation started.
Checking file:[...]/validator/src/main/resources/person.xml...
Referenced entity at "file:[...]/validator/src/main/resources/person.xsd".
cvc-complex-type.2.4.a: Invalid content was found starting with element 'first_name'. One of '{first_name}' is expected. [7]
XML validation finished.
EDIT: turns out I had a few misconceptions here about the meaning of 'targetnamespace' and other things.
The accepted answer worked - but @Ian Roberts pointed out (this is probably a genuine duplicate of the another post in fact) that the 'first_name' and 'last_name' were (the child elements of the 'person' element) were still (for some reason) regarded as being in no namespace at all.
Anyway: I have modified by XML and XSD like this - and this works - and I believe the (which is what I need) the elements are ALL in the person.com.test namespace here now:
<?xml version="1.0"?>
<p:person
xmlns:p="urn:person.com.test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:person.com.test person.xsd" >
<p:first_name>Joe</p:first_name>
<p:last_name>Bloggs</p:last_name>
</p:person>
This ALSO works in fact: (the original XML)
<person
xmlns="urn:person.com.test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:person.com.test person.xsd" >
<first_name>Joe</first_name>
<last_name>Bloggs</last_name>
</person>
So long as the XSD has the elementFormDefault="qualified" directive in it.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:person.com.test"
xmlns="urn:person.com.test"
elementFormDefault="qualified">
<xs:element name="person" type="Person" />
<xs:complexType name="Person">
<xs:sequence>
<xs:element name="first_name" type="xs:string" />
<xs:element name="last_name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>