0

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>
monojohnny
  • 5,894
  • 16
  • 59
  • 83
  • Have you checked any of the "related" questions linked on the right? Several of them explain the underlying problem nicely. – Ian Roberts Mar 04 '15 at 13:39
  • @Ian Roberts - I took a look at quite a few of the existing answers that showed up; but I didn't find one which had exactly the same error as mine.I stand corrected: actually this one http://stackoverflow.com/questions/25532389/cvc-complex-type-2-4-a-invalid-content-was-found-starting-with-element?rq=1 appears to be the same - I missed it. Thanks. – monojohnny Mar 04 '15 at 13:52
  • The issue is that local element declarations inside a `complexType` don't take on the `targetNamespace` by default, unless you add `elementFormDefault="qualified"` to the schema. – Ian Roberts Mar 04 '15 at 13:54
  • I didn't change that - and got it working from Xsitan's answer below. It seems I *had* to create a prefix for the namespace I was using rather than (as I thought I had) 'switching' the parent namespace (without a prefix) to the same namespace....I'm not sure what the difference is.... – monojohnny Mar 04 '15 at 13:57
  • The difference is that in your original XML document from the question, the `first_name` and `last_name` elements are in the same `urn:person.com.test` namespace as the root `person` element, whereas in Xsitan's version only the `person` element is in that namespace, the first and last name elements are not in a namespace. Xsitan's XML document is compatible with your existing schema, if you added `elementFormDefault="qualified"` to your schema then it would be compatible with your existing XML (but not with Xsitan's). – Ian Roberts Mar 04 '15 at 14:00
  • @Ian Roberts: can you please provide a specific link to one answer which this is an exact duplicate of - I can't find one (that has been answered at least) that matches exactly. (Granted: there are many similar ones, but not identical). – monojohnny Mar 04 '15 at 14:01
  • On reflection http://stackoverflow.com/q/17173383/592139 would have been a better question to mark as the duplicate. – Ian Roberts Mar 04 '15 at 14:02
  • Agreed: that is a closer match - but it was solved by the addition of 'elemenfFormDefault' - which (as it happens) turns out not to be my accepted answer here - the answer was add a prefix (for some reason still obscure to me) to my namespace – monojohnny Mar 04 '15 at 14:05
  • Read my previous comment - using the prefix changes the semantics of the XML because it moves the first name and last name elements out of the `urn:person.com.test` namespace. You've changed the XML to match the schema, the `elementFormDefault` would instead be the way to change the schema to match the XML. – Ian Roberts Mar 04 '15 at 14:06
  • ok - I read it : I thought I had done this already with 'targetNameSpace' - but I apparently misunderstood the meaning of this. I'll retry with the 'elementFormDefault' (which seems to match what I intended to mean in the first place...) - thanks. – monojohnny Mar 04 '15 at 14:15
  • I think you are right to mark this as a duplicate - it just took me a bit of staring and comparing to see that it is the same question - thanks for the input here. – monojohnny Mar 04 '15 at 14:27

1 Answers1

1

Declare the prefix of namespace xmlns:prefix="urn:person.com.test"

<?xml version="1.0"?>
<prefix:person  xmlns:prefix="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>
</prefix:person>

I validated by XMLSpear

Xstian
  • 8,184
  • 10
  • 42
  • 72
  • 1
    That worked - thank you - but why ? What is wrong with the way I had it originally ? – monojohnny Mar 04 '15 at 13:50
  • @monojohnny I'm sorry for the delay. The problem is that by default the elementFormDefault is qualified which indicates that elements from the target namespace must have the namespace prefix. [See here](http://www.w3schools.com/schema/el_schema.asp) – Xstian Mar 04 '15 at 17:28