0

When I try to validate this code, I get an error...

<?xml version="1.0"?>
<h:hotel xmlns:h="hotel">
    <h:existingRooms>
        <room>101</room>
        <room>102</room>
        <room>201</room>
    </h:existingRooms>
</h:hotel>

Error:

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

When I edit my XML to this, I don't get the error anymore:

<?xml version="1.0"?>
<h:hotel xmlns:h="hotel">
    <existingRooms>
        <room>101</room>
        <room>102</room>
        <room>201</room>
    </existingRooms>
</h:hotel>

My XSD is (with the error) is:

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:h="hotel"
    targetNamespace="hotel">
    <element name="hotel">
        <complexType>
            <sequence>                
                <element name="existingRooms">
                    <complexType>
                        <sequence>
                            <element name="room" type="integer" minOccurs="0" maxOccurs="unbounded"/>
                        </sequence>
                    </complexType>
                </element>      
            </sequence>
        </complexType>
    </element>
</schema>
stijnpiron
  • 359
  • 2
  • 4
  • 15

2 Answers2

1

If you add elementFormDefault="qualified" to the root of your schema,

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified"
        xmlns:h="hotel"
        targetNamespace="hotel">
  <element name="hotel">
    <complexType>
      <sequence>                
        <element name="existingRooms">
          <complexType>
            <sequence>
              <element name="room" type="integer" minOccurs="0" maxOccurs="unbounded"/>
            </sequence>
          </complexType>
        </element>      
      </sequence>
    </complexType>
  </element>
</schema>

Then you'll eliminate your unexpected error, but you'll next find the following error:

[Error] try.xml:6:15: cvc-complex-type.2.4.a: Invalid content was found starting with element 'room'. One of '{"hotel":room}' is expected.

But you probably anticipated that one and can easily correct it,

<?xml version="1.0"?>
<h:hotel xmlns:h="hotel"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="hotel try.xsd">
    <h:existingRooms>
        <h:room>101</h:room>
        <h:room>102</h:room>
        <h:room>201</h:room>
    </h:existingRooms>
</h:hotel>

and have your XML validating successfully against your XSD as requested.

See also:

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

It because implicitely in your schema elementFormDefault is set to unqualified.

It means that the schema you have set up is equivalent to the following declaration:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:h="hotel" targetNamespace="hotel" 
    elementFormDefault="unqualified" attributeFormDefault="unqualified">

Thus it is understood that only the global element is qualified, all the other elements remain unqualified.

If you require all elements to be qualified (to be in the namespace with uri hotel, you can modify your schema by setting elementFormDefault to qualified:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:h="hotel" targetNamespace="hotel" 
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

And the valid XML instance will be:

<?xml version="1.0"?>
<h:hotel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="hotel hotel.xsd" xmlns:h="hotel">
 <h:existingRooms>
  <h:room>101</h:room>
  <h:room>102</h:room>
  <h:room>201</h:room>
 </h:existingRooms>
</h:hotel>

just as this alternative instance:

<?xml version="1.0"?>
<hotel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="hotel hotel.xsd" xmlns="hotel">
 <existingRooms>
  <room>101</room>
  <room>102</room>
  <room>201</room>
 </existingRooms>
</hotel> 

If you only need the existingRooms element to be qualified, you set this to your schema:

<element name="existingRooms" form="qualified">

and your first XML you provided will be valid.

Please note:

  • usually, a namespace URI, should be in the form http://some.url/hotel
potame
  • 7,597
  • 4
  • 26
  • 33
  • Yes, the last option is the solution of my problem :-) What is the advantage of the URI form you advise, above the form I use? – stijnpiron Nov 16 '15 at 17:16
  • There's no specific advantage. The value of a namespace should be URI like specified here http://www.w3.org/TR/REC-xml-names/ – potame Nov 17 '15 at 10:05