2017-02-28 55 views
1

我想將斷言添加到XSD,但將斷言保存在單獨的模式文件中。這是因爲XSD由第三方提供,並且新版本經常發佈。將XSD斷言保存在單獨的模式中

第三方XSD(SchoolModule.xsd):

<?xml version="1.0" encoding="UTF-8"?> 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.1" id="SchoolModule" 
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1"> 

    <xs:include schemaLocation="../Includes/Address_Structure.xsd"/> 

    <xs:element name="School" type="SchoolStructure"/> 

    <xs:complexType name="SchoolStructure"> 
     <xs:sequence> 
      <xs:element name="Headteacher" type="xs:String" minOccurs="0"/> 
      <xs:element name="SchoolCharacteristics" type="SchoolCharacteristicsStructure"/> 
     </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="SchoolCharacteristicsStructure"> 
     <xs:sequence> 
      <xs:element name="SchoolName" type="SchoolNameType"/> 
      <xs:element name="SchoolType" type="SchoolTypeType"/> 
      <xs:element name="NumberOfPlaces" minOccurs="0"> 
       <xs:simpleType> 
        <xs:restriction base="xs:integer"> 
         <xs:minInclusive value="0"/> 
         <xs:maxInclusive value="999"/> 
         <xs:totalDigits value="3"/> 
         <xs:fractionDigits value="0"/> 
        </xs:restriction> 
       </xs:simpleType> 
      </xs:element> 
      <xs:element name="IntakeType" type="xs:boolean"/> 
     </xs:sequence> 
    </xs:complexType> 
    ... 
</xs:schema> 

這是我嘗試在獨立的文件中的內容:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" version="1.1" id="Assertions" 
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1"> 
    <xs:include schemaLocation="SchoolModule.xsd"/> 

    <xs:element name="assertions"> 
     <xs:complexType> 
      <xs:assert test="/Message/School/SchoolCharacteristics/SchoolType ='Secondary' and /Message/School/Headteacher"/> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

當我試圖使斷言如果SchoolType = Secondary,則Headteacher元素應該只存在。但是,即使我將XML文件中的SchoolType設置爲Secondary和Headteacher元素以外的東西,我的XML文件已驗證成功,但這似乎並不奏效。有人能指出我要去哪裏嗎?

注意:我已驗證斷言中的XPath是正確的並返回預期值。

回答

1

XSD斷言可能只在相關元素的內容模型上。您無法從xs:assert全侷限制標記。

您可以改爲編寫XSLT,將原始XSD轉換爲包含正確位置中的斷言的XSD。但是,您必須注意,您所說的經常更改的原始XSD與您的聲明保持一致(或相應地調整您的聲明)。