XML文件,即使它违反XSD

XML文件,即使它违反XSD,xml,validation,xsd,xsd-validation,Xml,Validation,Xsd,Xsd Validation,我创建了以下架构以验证我的xml文档: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="idType"> <xs:restriction base="xs:ID"> <xs:pattern value="IT\d{2}-\d{3}-\

我创建了以下架构以验证我的xml文档:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="idType">
    <xs:restriction base="xs:ID">
        <xs:pattern value="IT\d{2}-\d{3}-\d{3}"/>
    </xs:restriction>
</xs:simpleType>
<xs:simpleType name="codeType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="MP"/>
        <xs:enumeration value="SP"/>
         <xs:enumeration value="WPA"/>
    </xs:restriction>
 </xs:simpleType>
 <xs:element name="degrees">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="degree" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
     </xs:complexType>
</xs:element>
 <xs:attribute name="degreeID" type="idType"/>
 <xs:attribute name="degreeCode" type="codeType"/>
 <xs:element name="title" type="xs:string"/>
 <xs:element name="approvalDate" type="xs:date"/>
 <xs:element name="effectiveDate" type="xs:date"/>
 <xs:element name="summary" type="xs:string"/>
<xs:element name="coordinator" type="xs:string"/>
<xs:element name="comment">
     <xs:complexType>
        <xs:simpleContent>
             <xs:extension base="xs:string">
                 <xs:attribute name="date" type="xs:date" use="required"/>
             </xs:extension>
        </xs:simpleContent>
     </xs:complexType>
</xs:element>
<xs:element name="degree">
    <xs:complexType>
         <xs:sequence>
             <xs:element ref="title"/>
            <xs:element ref="approvalDate" minOccurs="0" maxOccurs="1"/>
            <xs:element ref="effectiveDate"/>
            <xs:element ref="summary"/>
            <xs:element ref="coordinator"/>
             <xs:element ref="comment" minOccurs="0" maxOccurs="unbounded"/>
         </xs:sequence>
        <xs:attribute name="degreeID" use="required"/>
        <xs:attribute name="degreeCode" use="required"/>
    </xs:complexType>
</xs:element>

该要求的关键是顺序,我认为该顺序要求元素按顺序排列,除非另有说明,否则每个元素只出现一次

我的模式和XML格式良好,XML(如下)根据XMLSpy的模式进行验证

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<degrees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="degrees.xsd">
   <degree degreeID="IT10-152-100" degreeCode="MP">
      <title>Information Technology-Mobile Programmer</title>
      <approvalDate>2017-01-12</approvalDate>
      <effectiveDate>2017-09-01</effectiveDate>
      <summary>This two-year program meets the specific skill and knowledge requirements
    of technical and professional jobs within the information technology field for
    an entry-level mobile programmer working in a small to medium size
    organization. Training blends general educational development with required
    IT technical skills. Emphasis is in mobile web and application development.
      </summary>
      <coordinator>Janis Wu</coordinator>
      <comment date="2017-01-12">Janis Wu contacted regards heads up on approval.  Official paperwork is being sent</comment>
      <comment date="2017-01-01">Janis Wu assigned as coordinator</comment>
      <comment date="2016-12-01">Application draft submitted</comment>
   </degree>

</degrees>

信息技术移动程序员
2017-01-12
2017-09-01
该两年制课程满足特定的技能和知识要求
信息技术领域的技术和专业工作
一个入门级的移动程序员,在中小型计算机中工作
组织。培训将普通教育发展与所需技能相结合
IT技术技能。重点是移动web和应用程序开发。
吴佳妮
Janis Wu就批准事宜联系了负责人。官方文件正在发送中
吴佳妮被任命为协调员
提交的申请草案

但是,当我操作XML(添加和附加批准或生效日期元素,在日期元素之间添加注释)时,XML仍然有效。但我不明白为什么。我知道XSD引用是正确的,因为如果我从注释中删除所需的日期属性,它将不会验证。

在XML模式中,您正在声明一个名为
degree
的元素序列。 您不会将名为
degree
的元素声明为任何类型的,因此默认情况下,它属于任何复杂类型都是正确的,因此您也可以在XML
degree
元素的
degree元素中包含任何随机属性或子元素,并且它仍将进行验证

看起来你真正想要的是这样的

<xs:element name="degrees">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="degree" type="DEGREE_TYPE" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
     </xs:complexType>
</xs:element>

也会改变

<xs:element name="degree">
    <xs:complexType>
     etc
    </xs:complexType>
</xs:element>
用于:


您正在使用本地xsd。有一个关于如何引用它的问题。如果degree元素未声明为任何类型,请选中此复选框,这样仅作为degree标记就正确了。您可以在degree元素中包含更有效的date元素,也可以在degree元素中包含任何属性或任何元素子元素,并且仍然正确。另一个更正的选项是在名为degrees
的序列中更改
对于
我很高兴它很有用:)
<xs:complexType name="DEGREE_TYPE">
 etc
</xs:complexType>