2012-05-24 23 views
0

假設我有我的模式自定義數據類型:如何在我的XML模式中需要自定義數據類型的特定實例?

<xs:element name="Fruit"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element ref="Name"/> 
     <xs:element ref="Supplier"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

    <xs:element name="Name" type="xs:NCName"/> 
    <xs:element name="Supplier" type="xs:string"/> 

其他地方我想要求水果的特定實例的架構,所以我有類似如下的XML文件:

<fruits> 
    <common> 
    <!-- the common section should always include Fruits with these Names --> 
    <Fruit> 
     <Name>apple</Name> 
     <Supplier>Shady Orchard</Supplier> 
    </Fruit> 
    <Fruit> 
     <Name>orange</Name> 
     <Supplier>Florida Orange Co.</Supplier> 
    </Fruit> 
    <Fruit> 
     <Name>banana</Name> 
     <Supplier>Bananaland</Supplier> 
    </Fruit> 
    </common> 
    <exotic> 
    <Fruit> 
     <Name>kiwano</Name> 
     <Supplier>Fancy Fruits</Supplier> 
    </Fruit> 
    <Fruit> 
     <Name>rambutan</Name> 
     <Supplier>Indonesia Fruit Co.</Supplier> 
    </Fruit> 
    <!-- the list goes on... --> 
    </exotic> 
</fruits> 

我知道我可以定義在我的文件中像這樣的異國部分:

<xs:element name="exotic"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element maxOccurs="unbounded" ref="Fruit"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

但是我怎麼定義共同部分,這樣Fruit s的名字蘋果,橙色香蕉總是需要?

+0

我不知道我明白。您希望創建元素,該元素僅允許將某些類型的水果作爲子元素,並且允許接受所有水果的元素。是對的嗎?或者,您可能希望元素實際上需要三種最受歡迎​​的水果 – toniedzwiedz

+0

是的,這是您提到的最後一件事,我堅持 - 我希望元素需要這三個特定的成果。 – rob

+0

不要以爲你可以在xsd中做到這一點,當然我不能想出一種定義這種方式的方法。 Schematron可以驗證,除此之外,你需要蘋果橙和香蕉作爲水果類型的元素,然後你可以用序列 –

回答

1

解決問題並不難。我很確定有一個更優雅的解決方案,但這會起作用。唯一的是,我們並不需要特定的實例,但創建專門的類型來匹配這些值。我很確定有一種方法可以不通過固定值來實現類型限制,但我不記得這個屬性,我不知道它是否可以使用三個值的替代值。

這裏有一個xsd

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/FruitSchema" 
xmlns:fr="http://www.example.org/FruitSchema" xmlns:tns="http://www.example.org/FruitSchema" 
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<xs:element name="fruits"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="fr:common" minOccurs="1" maxOccurs="1"/> 
      <xs:element ref="fr:exotic" minOccurs="1" maxOccurs="1" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:element name="fruit"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="fr:name" /> 
      <xs:element ref="fr:supplier" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:element name="name" type="xs:NCName" /> 
<xs:element name="supplier" type="xs:string" /> 

<xs:element name="exotic"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element maxOccurs="unbounded" ref="fr:fruit" /> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:element name="common"> 
    <xs:complexType> 
     <xs:sequence minOccurs="0" maxOccurs="unbounded"> 
      <xs:element name="fruit"> 
       <xs:complexType> 
        <xs:sequence minOccurs="1" maxOccurs="1"> 
         <xs:element name="name" type="fr:CommonFruitType" /> 
         <xs:element ref="fr:supplier" /> 
        </xs:sequence> 
       </xs:complexType> 
      </xs:element> 
     </xs:sequence> 
    </xs:complexType> 
</xs:element> 

<xs:simpleType name="CommonFruitType"> 
    <xs:restriction base="xs:NCName"> 
     <xs:enumeration value="apple" /> 
     <xs:enumeration value="orange" /> 
     <xs:enumeration value="banana" /> 
    </xs:restriction> 
</xs:simpleType> 

,這裏是驗證失敗的例子。

<?xml version="1.0" encoding="UTF-8"?> 
<fruits xmlns="http://www.example.org/FruitSchema" xmlns:fr="http://www.example.org/FruitSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.example.org/FruitSchema FruitSchema.xsd"> 
    <common> 
     <!-- the common section should always include fruits with these names --> 
     <fruit> 
      <name>apple</name> 
      <supplier>Shady Orchard</supplier> 
     </fruit> 
     <fruit> 
      <name>apple</name> 
      <supplier>Shady Orchard</supplier> 
     </fruit> 
     <fruit> 
      <name>orange</name> 
      <supplier>Florida Orange Co.</supplier> 
     </fruit> 
     <fruit> 
      <name>banana</name> 
      <supplier>Bananaland</supplier> 
     </fruit> 
     <fruit> 
      <name>kiwano</name> 
      <supplier>Fancy fruits</supplier> 
     </fruit> 
    </common> 
    <exotic> 
     <fruit> 
      <name>kiwano</name> 
      <supplier>Fancy fruits</supplier> 
     </fruit> 
     <fruit> 
      <name>rambutan</name> 
      <supplier>Indonesia fruit Co.</supplier> 
     </fruit> 
     <!-- the list goes on... --> 
    </exotic> 
</fruits> 

它只是驗證了common那些果實,其是蘋果,橙子或香蕉,無論供應商是什麼。

+0

做到這一點。我會記住這一點。 –

相關問題