Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Validation: "No Child Element Is Expected At This Point"

I'm trying to develop an XSD grammar according to a given XML file. The given XML file itemList.xml is shown as below.

<?xml version="1.0" encoding = "utf-8"?>
<itemList 
    xmlns="http://www.w3schools.com" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.w3schools.com  itemList.xsd" >
     <item>spoon</item>  
     <item>knife</item>
     <item>fork</item>  
     <item>cup</item>
</itemList>

The itemList.xsd file that I developed is shown as below.

<schema 
    xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:co="http://www.w3schools.com"
    targetNamespace="http://www.w3schools.com" 
    elementFormDefault="qualified">
    <simpleType name="itemType">
        <restriction base="string"/>
    </simpleType>
    <complexType name="itemListType">
        <sequence>
            <element name="item" type="co:itemType"/>
        </sequence>
    </complexType>
    <element name="itemList" type="co:itemListType"/>
</schema>

When I validate the XML against the XSD using this XML validator, I get the error

Cvc-complex-type.2.4.d: Invalid Content Was Found Starting With Element 'item'. No Child Element Is Expected At This Point.. Line '6', Column '12'.

It seems that I should rewrite my complexType in itemList.xsd, but I'm not sure what to do. Many thanks to whoever could help.

like image 319
goldfrapp04 Avatar asked Jan 24 '14 19:01

goldfrapp04


2 Answers

Your itemList is currently made of exactly one item; that is because the default particle cardinality is 1 (minOccurs = maxOccurs = 1).

If you wish more than one, then you need to add maxOccurs attribute with the appropriate number; for unlimited, use maxOccurs="unbounded"... like so:

<element name="item" type="co:itemType" maxOccurs="unbounded"/>
like image 119
Petru Gardea Avatar answered Oct 14 '22 06:10

Petru Gardea


In my case I got this message because the ordering of the fields in my XML did not match those in my XSD, I had erroneously reversed the order of the last two fields.

While this is not the situation in the question, I thought it may help others who are drawn to this question by the title (doubtless I will reference this question again in the future when I have forgotten what I have just learned).

like image 23
Dave Richardson Avatar answered Oct 14 '22 05:10

Dave Richardson