2012-02-08 65 views
0

所以我在檢查屬性時遇到問題。到目前爲止,我已經嘗試了我所知道的一切,但它沒有奏效。如果易腐=否有一些額外的元素,如果是的話那麼就有一些不同的元素來自易腐=不。我試過團體,但它沒有奏效。我正在考慮將食物和庫存分組,但是對於易腐爛的食物有限制。只有在易腐爛的情況下,我們纔有食物元素,如果不存在,我們就有存貨元素。請幫助!!!XML Schema構造,具有不同值的相同元素的xml需要架構

<product id = "p12" perishable = "yes"> 
    <name>Ice cream</name> 
    <manufacturer>xsz Co.</manufacturer> 
    <quantity>25</quantity> 
    <price>2</price> 

    <food> 
     <nutrition> 
      <calcium>10.30</calcium> 
      <proteins>35.5</proteins> 
      <fat>10</fat> 
     </nutrition> 

     <expirationDate>2000-09-12</expirationDate> 
    </food> 
</product> 

<product id = "p13" perishable = "no"> 
    <name>AA Battries</name> 
    <manufacturer>DCells</manufacturer> 
    <quantity>100</quantity> 
    <price>4</price> 

    <stock> 
     <warehouse id = "w12"> 
     xsz warehouse 
      <stock>25000</stock> 
     </warehouse> 

     <warehouse id = "w13"> 
     rza warehouse 
      <stock>5000</stock> 
     </warehouse> 
    </stock> 

</product> 

<!-- defining the nutrition element for the perishable product --> 

<xs:element name="nutrition"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element name="calcium" type="xs:decimal"/> 
      <xs:element name="proteins" type="xs:decimal"/> 
      <xs:element name="fat" type="xs:decimal"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<!-- defining the group product that is perishable --> 

<xs:group name="perishableGroup"> 
    <xs:sequence> 
     <xs:element name="product"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="name" type="xs:string"/> 
        <xs:element name="manufacturer" type="xs:string"/> 
        <xs:element name="quantity" type="xs:positiveInteger"/> 
        <xs:element name="price" type="xs:decimal"/> 
        <xs:element name="food"> 
         <xs:complexType> <!-- defining the food element for perishable product --> 
          <xs:sequence> 
           <xs:element ref="nutrition"/> <!-- defined above --> 
           <xs:element name="expirationDate" type="xs:date"/> 
          </xs:sequence> 
         </xs:complexType> 
        </xs:element> 
       </xs:sequence> 
       <xs:attribute name="id" type="xs:ID" use="required"/> 
       <xs:attribute name="perishable" type="xs:string" use="required" fixed="yes"/> 
      </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
</xs:group> 

<!-- defining the group product that is nonperishable --> 

<xs:group name="nonperishableGroup"> 
    <xs:sequence> 
     <xs:element name="product"> 
      <xs:complexType> 
       <xs:sequence> 
        <xs:element name="name" type="xs:string"/> 
        <xs:element name="manufacturer" type="xs:string"/> 
        <xs:element name="quantity" type="xs:positiveInteger"/> 
        <xs:element name="price" type="xs:decimal"/> 
        <xs:element name="stock"> 
         <xs:complexType> 
          <xs:sequence> 
           <xs:element name="warehouse" maxOccurs="unbounded"> 
            <xs:complexType mixed="true"> 
             <xs:sequence> 
              <xs:element name="stock" type="xs:positiveInteger"/> 
             </xs:sequence> 
             <xs:attribute name="id" type="xs:ID" use="required"/> 
            </xs:complexType> 
           </xs:element> 
          </xs:sequence> 
         </xs:complexType> 
        </xs:element> 
       </xs:sequence> 
       <xs:attribute name="id" type="xs:ID" use="required"/> 
       <xs:attribute name="perishable" type="xs:string" use="required" fixed="no"/> 
      </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
</xs:group> 

<xs:element name="products"> 
    <xs:complexType> 
     <xs:choice minOccurs="1" maxOccurs="unbounded"> 
      <xs:group ref="perishableGroup"/> 
      <xs:group ref="nonperishableGroup"/> 
     </xs:choice> 
    </xs:complexType> 
</xs:element> 

回答

0

不能使用屬性來控制元素的內容模型,但有一個例外:在xsi:type屬性。 xsi:type屬性可以出現在您的實例文檔中。它告訴驗證解析器應該使用什麼類型來驗證元素。指定的類型必須是元素聲明類型的子類型(或聲明的類型本身)。然後你會定義兩個相關的類型(一個擴展或限制另一個,否則擴展或限制一些常用的抽象基礎)。其中一種類型可以支持你有易腐物品的情況,另一種類型的情況下你有一個不易腐爛的物品。

但是,沒有辦法使用易腐性屬性來控制它。除非你可能使用XSD 1.1的斷言(我不知道)。最後我檢查了XSD 1.1仍然只是一個草稿,但如果這是您的選擇,您可以查看它。我相信一些工具對它有支持(Saxon。Xerces?)

+0

P.S.如果您可以改變,請將您的易腐屬性更改爲易腐和非易腐元素之間的選擇。我想,這會讓你更容易。 – Kevin 2012-02-09 19:23:18

+0

我必須按原樣驗證xml文檔。我不能使用易腐爛和不易腐爛的東西,它必須是易腐爛的。非常感謝 – Keisha 2012-02-09 22:03:59

相關問題