简体   繁体   中英

Why won't my XML schema pick up the errors?

My schema wont pick up the errors in my data, is there a glaring reason? Please see below, essentially for the restrictions I created it should come up with errors when other information has been given, but that is not the case with mine. It just appears valid. Here I applied a restriction using type string and ennumerating it. It should throw validation errors because the spelling is incorrect in some places.

Shorten version: 

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Midterm">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="StudentData" maxOccurs="unbounded" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:string" name="Gnumber"/>
                            <xs:element type="xs:string" **name="ResidenceStatus"/>
                            **<xs:simpletype>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="In-state"/>
                                    <xs:enumeration value="Out-of-state"/>
                                </xs:restriction>
                            </xs:simpletype>****
                            <xs:element type="xs:string" name="FirstName"/>
                            <xs:element type="xs:string" name="MiddleInitial"/>
                            <xs:element type="xs:string" name="LastName"/>
                            <xs:element type="xs:string" name="DOB"/>
                            <xs:element type="xs:string" name="ProgramName"/>
                            <xs:element type="xs:string" name="Concentration"/>
                            <xs:element type="xs:float" name="StartYear"/>
                            <xs:element name="course">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element type="xs:string" name="Department"/>
                                        <xs:element type="xs:float" name="CatalogNumber"/>
                                        <xs:element type="xs:string" name="Semester"/>
                                        <xs:element type="xs:float" name="Year"/>
                                        <xs:element type="xs:string" name="LetterGrade"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XML DATA

<Midterm>
    <StudentData>
        <Gnumber>   G12654312   </Gnumber>
        <ResidenceStatus>   In-state    </ResidenceStatus>
        <FirstName> ALBERTO </FirstName>
        <MiddleInitial> L   </MiddleInitial>
        <LastName>  SMITH   </LastName>
        <DOB>   2/3/1981    </DOB>
        <ProgramName>   MS Health Informatics   </ProgramName>
        <Concentration> Data analytics  </Concentration>
        <StartYear> 2014    </StartYear>
        <course> 
            <Department>    HAP </Department>
            <CatalogNumber> 463 </CatalogNumber>
            <Semester>  SPRING  </Semester>
            <Year>  2014    </Year>
            <LetterGrade>   B+  </LetterGrade>
        </course>
    </StudentData>
</Midterm>

Trying to validate your sample XML with the XSD, it threw some errors, so I couldn't reproduce your problem, but only fix the errors of your XSD. I used the command

xmlstarlet val --err --xsd source.xsd input.xml

to validate your XSD. XSD-1.0 seemed to suffice to validate it, so no XSD-1.1 processor was necessary.


One of the problems of your source XML seemed to be the leading and trailing spaces of the value of ResidenceStatus .

Applying your XSD to the XML lead to errors until I fixed the definition of ResidenceStatus . So far, I cannot tell if this occurs due to your XSD processor.

But you can try to fix the definition to

<xs:element name="ResidenceStatus">
  <xs:simpleType>
    <xs:restriction base="normalized">
        <xs:enumeration value="In-state"/>
        <xs:enumeration value="Out-of-state"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

and add the type definition of normalized at the end of the root level of your xs:schema :

<xs:simpleType name="normalized">
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="collapse"/>
  </xs:restriction>
</xs:simpleType>

This simpleType named normalized will remove leading and trailing spaces of the element's value, so that the enumeration will match.

PS: your XSD had a typo:
<xs:simpletype> of ResidenceStatus should have been <xs:simpleType> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM