2012-05-24 24 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

做到这一点。我会记住这一点。 –

相关问题