Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xsd 如何在XML模式中要求自定义数据类型的特定实例?_Xsd - Fatal编程技术网

Xsd 如何在XML模式中要求自定义数据类型的特定实例?

Xsd 如何在XML模式中要求自定义数据类型的特定实例?,xsd,Xsd,假设我的架构中有一个自定义数据类型: <xs:element name="Fruit"> <xs:complexType> <xs:sequence> <xs:element ref="Name"/> <xs:element ref="Supplier"/> </xs:sequence> </xs:complexType> </

假设我的架构中有一个自定义数据类型:

  <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>


但是我如何定义公共部分,以便始终需要名为apple、orange和banana的
水果
s?

这并不难解决。我很确定有一个更优雅的解决方案,但这会奏效。唯一的问题是,我们实际上并不需要特定的实例,而是创建专门的类型来只匹配这些值。我很确定有一种方法可以通过固定值来实现无类型限制,但我记不起该属性,也不知道它是否适用于三个值的替代值

这是一个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
中的水果,即苹果、橙子或香蕉,无论供应商是什么。

我不确定我是否理解。您希望创建一个只允许某些类型的水果作为子元素的元素和一个接受所有水果的元素。是这样吗?或者你想让元素实际需要三个最流行的结果是的,这是你提到的最后一件我一直坚持的事情——我想让元素需要这三个特定的结果。不要认为你可以在xsd中做到这一点,当然我想不出定义它的方法。Schematron可以验证这一点,除此之外,你需要苹果橙和香蕉作为水果类型的元素,然后你可以用sequenceNow进行验证,这很好。我会记住的。