Search code examples
xmlxsdxsd-validationxml-validation

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


I'm trying to write a simple XML file with a simple XSD.

Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<forecast week="June 1st 2016"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="forecast.xsd">
    <day name="Monday">
        <HiTemp>84</HiTemp> 
        <LoTemp>74</LoTemp> 
        <Wind>SSE 18 mph</Wind> 
        <Humidity>59%</Humidity> 
        <DewPoint>68 degrees</DewPoint> 
        <Visibility>10 miles</Visibility> 
        <UVIndex>4 of 10</UVIndex> 
        <Sunrise>6:01 AM</Sunrise>
        <Sunset>8:49 PM</Sunset>
    </day>
    <day name="Tuesday">
        <HiTemp>89</HiTemp> 
        <LoTemp>77</LoTemp> 
        <Wind>NW 6 mph</Wind> 
        <Humidity>68%</Humidity> 
        <DewPoint>54 degrees</DewPoint> 
        <Visibility>8 miles</Visibility> 
        <UVIndex>6 of 10</UVIndex> 
        <Sunrise>6:03 AM</Sunrise>
        <Sunset>8:51 PM</Sunset>
    </day>
    <day name="Wednesday">
        <HiTemp>91</HiTemp> 
        <LoTemp>76</LoTemp> 
        <Wind>NE 27 mph</Wind> 
        <Humidity>48%</Humidity> 
        <DewPoint>44 degrees</DewPoint> 
        <Visibility>10 miles</Visibility> 
        <UVIndex>8 of 10</UVIndex> 
        <Sunrise>6:05 AM</Sunrise>
        <Sunset>8:53 PM</Sunset>
    </day>
    <day name="Thursday">
        <HiTemp>91</HiTemp> 
        <LoTemp>74</LoTemp> 
        <Wind>SSE 20 mph</Wind> 
        <Humidity>61%</Humidity> 
        <DewPoint>68 degrees</DewPoint> 
        <Visibility>7 miles</Visibility> 
        <UVIndex>5 of 10</UVIndex> 
        <Sunrise>6:07 AM</Sunrise>
        <Sunset>8:55 PM</Sunset>
    </day>
    <day name="Friday">
        <HiTemp>97</HiTemp> 
        <LoTemp>76</LoTemp> 
        <Wind>SE 10 mph</Wind> 
        <Humidity>79%</Humidity> 
        <DewPoint>54 degrees</DewPoint> 
        <Visibility>10 miles</Visibility> 
        <UVIndex>8 of 10</UVIndex> 
        <Sunrise>6:10 AM</Sunrise>
        <Sunset>8:57 PM</Sunset>
    </day>
</forecast>

And my XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
    vc:minVersion="1.1">

 <xs:element name="forecast">
     <xs:complexType>
         <xs:sequence maxOccurs="unbounded">
             <xs:element name="HiTemp" type="xs:integer"/>
             <xs:element name="Lotemp" type="xs:integer"/>
             <xs:element name="Wind" type="xs:string"/>
             <xs:element name="Humidity" type="xs:string"/>
             <xs:element name="Dewpoint" type="xs:string"/>
             <xs:element name="Visibility" type="xs:string"/>
             <xs:element name="UVIndex" type="xs:string"/>
             <xs:element name="Sunrise" type="xs:time"/>
             <xs:element name="Sunset" type="xs:time"/>
         </xs:sequence>
         <xs:attribute name="week" type="xs:string" use="required"/>
     </xs:complexType>
   </xs:element>
</xs:schema>

The error that I'm getting in oXygen is

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

It's probably something simple. I've been searching other answers but I can't pinpoint what's causing this on mine, as it usually had something to do with something else on the other answers.


Solution

  • Your XSD required several adjustments, including:

    • Added missing day wrapper element with name attribute.
    • Corrected case of Lotemp and Dewpoint.

    Here is your XSD updated to be able to validate your XML successfully:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
               elementFormDefault="qualified"
               vc:minVersion="1.1">
    
      <xs:element name="forecast">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="day" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="HiTemp" type="xs:integer"/>
                  <xs:element name="LoTemp" type="xs:integer"/>
                  <xs:element name="Wind" type="xs:string"/>
                  <xs:element name="Humidity" type="xs:string"/>
                  <xs:element name="DewPoint" type="xs:string"/>
                  <xs:element name="Visibility" type="xs:string"/>
                  <xs:element name="UVIndex" type="xs:string"/>
                  <xs:element name="Sunrise" type="xs:string"/>
                  <xs:element name="Sunset" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="name" type="xs:string" use="required"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:attribute name="week" type="xs:string" use="required"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>