如何在 xs:all 中创建 xs:choice - (xsd:schema)

Nis*_*ant 0 xsd xsd-validation

我想验证以下数组中的数据:

input_array = array(
  "boy"=> array("boy_id"=>1),
   "first_name=>"First Name",
   "last_name"=>"Last Name"
);
Run Code Online (Sandbox Code Playgroud)

input_array 内部的第一个索引可以替换为 girls 数组,如下所示

 "girl"=>array("girl_id"=>2)
Run Code Online (Sandbox Code Playgroud)

我想创建 xsd 来验证信息,如下所示:

 <xs:element name="xml">
    <xs:complexType>
      <xs:all>
        <xs:element ref="boy" minOccurs="0"/>
        <xs:element ref="girl" minOccurs="0"/>
        <xs:element ref="first_name"/>
        <xs:element ref="last_name"/>
      </xs:all>
    </xs:complexType>
  </xs:element>
Run Code Online (Sandbox Code Playgroud)

问题 - 我想确保男孩或女孩信息存在,first_name并且last_name将永远存在,我如何将它们(女孩,男孩)作为选择或选项。我更愿意使用xs:all这样元素顺序不应该成为问题。

我推荐使用此链接,以便尝试在内部使用选择xs:all,但无法使其工作。我将不胜感激任何回应。谢谢。

Col*_*ion 5

在您引用的文章(http://www.w3.org/wiki/Needs_choice_inside_all)中,他们提供了一个带有替换组的示例。那么,为什么不使用一个呢?

它应该是这样的:

<xs:element name="xml">
  <xs:complexType>
    <xs:all>
      <xs:element ref="gender" minOccurs="1"/>
      <xs:element ref="first_name"/>
      <xs:element ref="last_name"/>
    </xs:all>
  </xs:complexType>
</xs:element>

<xs:element name="gender" abstract="true"/>
<xs:element name="boy" substitutionGroup="gender"> ... </xs:element>
<xs:element name="girl" substitutionGroup="gender"> ... </xs:element>
Run Code Online (Sandbox Code Playgroud)

具体来说,我尝试了这个完整的架构:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="xml">
    <xs:complexType>
      <xs:all>
        <xs:element ref="gender" minOccurs="1"/>
        <xs:element ref="first_name"/>
        <xs:element ref="last_name"/>
      </xs:all>
    </xs:complexType>
  </xs:element>

  <xs:element name="gender" abstract="true"/>
  <xs:element name="boy" substitutionGroup="gender"/>
  <xs:element name="girl" substitutionGroup="gender"/>

  <xs:element name="first_name" type="xs:string"/>
  <xs:element name="last_name" type="xs:string"/>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)

验证此 XML:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <girl/>
  <first_name>Lara</first_name>
  <last_name>Croft</last_name>
</xml>
Run Code Online (Sandbox Code Playgroud)

有效!那时,如果代替<girl/>我指定的<boy/>,它也会通过,但当它们既不存在也不<girl/>存在<boy/>或任何一个在一起时则不会通过。