2014-07-25 26 views
0

我創建了下面的模式來驗證我的XML文檔:XML文件,即使它違反了XSD

<?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引用是正確的,因爲如果我從註釋中刪除所需的日期屬性,它不會驗證。

+0

您使用的是當地的xsd。如何引用它有一個問題。檢查這個http://stackoverflow.com/questions/19253402/how-to-reference-a-local-xml-schema-file-correctly – alkis

+0

度數元素它沒有被聲明爲任何類型,所以它是正確的作爲學位標籤。您可以在度數元素中包含更多effectiveDate元素,並且您還可以在度數元素中擁有任何屬性或任何元素子元素,並且它仍然是正確的。 – sergioFC

回答

0

在您的XML Schema中,您聲明瞭一個名爲degree的元素序列。 你幾乎不申報叫degree是任何類型的的元素,所以默認情況下它是正確的福祉任何複雜類型,所以你也可以有在degree元素你的XML 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> 
+0

糾正它的另一個選擇是在sergioFC感謝' sergioFC

+0

,在名爲degrees 的序列內部進行更改。你的回答是我的問題100%! – spike

+0

我很高興這很有用:) – sergioFC