XML文件,即使它违反了XSD

时间:2014-07-25 13:26:11

标签: 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}-\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>

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

1 个答案:

答案 0 :(得分:0)

在XML Schema中,您声明了一系列名为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>

有:

<xs:complexType name="DEGREE_TYPE">
 etc
</xs:complexType>