简体   繁体   English

为什么我的XML模式无法提取错误?

[英]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 XML数据

<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. 尝试使用XSD验证示例XML时,它引发了一些错误,因此我无法重现您的问题,只能修复XSD的错误。 I used the command 我用了这个命令

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

to validate your XSD. 验证您的XSD。 XSD-1.0 seemed to suffice to validate it, so no XSD-1.1 processor was necessary. XSD-1.0似乎足以验证它,因此不需要XSD-1.1处理器。


One of the problems of your source XML seemed to be the leading and trailing spaces of the value of ResidenceStatus . 您的源XML问题之一似乎是ResidenceStatus价值的主要和尾随空格。

Applying your XSD to the XML lead to errors until I fixed the definition of ResidenceStatus . 将XSD应用于XML会导致错误,直到我修复了ResidenceStatus的定义。 So far, I cannot tell if this occurs due to your XSD processor. 到目前为止,我无法确定这是否由于您的XSD处理器而发生。

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:schema的根级别末尾添加normalized的类型定义:

<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. 这个名为normalized simpleType将删除元素值的前导和尾随空格,以便枚举匹配。

PS: your XSD had a typo: PS:您的XSD有拼写错误:
<xs:simpletype> of ResidenceStatus should have been <xs:simpleType> . ResidenceStatus <xs:simpletype>应该是<xs:simpleType>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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