选择题如何设计xsd?

How to design an xsd for multiple choice questions?

现在我有一个允许用户创建多项选择题的系统。用户(理想情况下)会像这样使用它们:

<MultipleChoice>
   <Choices>
      <Choice>a) apple</Choice>
      <Choice>b) banana</Choice>
      <Choice correct="true">c) grapes</Choice>
   </Choices>
</MultipleChoice>

注意葡萄的正确属性。这是为了当我在 UI 中渲染它时,它旁边有一个绿色的复选标记。但是,对于我当前的 xsd,我无法阻止用户根本不输入任何 correct="true" 属性或将其用于多项选择。

我不希望用户能够执行的操作示例如下:

没有正确的选择(不要让用户这样做)

<MultipleChoice>
   <Choices>
      <Choice>a) apple</Choice>
      <Choice>b) banana</Choice>
      <Choice>c) grapes</Choice>
   </Choices>
</MultipleChoice>

多个正确选择(不要让用户这样做)

<MultipleChoice>
   <Choices>
      <Choice>a) apple</Choice>
      <Choice correct="true">b) banana</Choice>
      <Choice correct="true">c) grapes</Choice>
   </Choices>
</MultipleChoice>

如何限制我的模式的用户只能定义一个正确的选择?我的意思是,如果他们违反了一个且只有一个的正确答案规则,我应该能够给他们一个错误。

这是我当前的架构

...
<xs:complexType name="MultipleChoice">
   <xs:complexContent>
       <xs:element name="Choices" type="Choices" minOccurs="1" maxOccurs="1"/>
   </xs:complexContent>
</xs:complexType>

<xs:complexType name="Choices">
   <xs:sequence>
       <xs:element name="Choice" type="Choice" maxOccurs="unbounded" minOccurs="1"/>
   </xs:sequence>
</xs:complexType>

<xs:complexType name="Choice">
   <xs:simpleContent>
       <xs:extension base="xs:string">
           <xs:attribute name="correct" type="xs:boolean" default="false"/>
       </xs:extension>
   </xs:simpleContent>
</xs:complexType>

最简单的方法是将其定义为单独的元素并将其限制为仅供一个元素使用。所以我的建议是这样定义它:

<xs:complexType name="MultipleChoice">
   <xs:complexContent>
       <xs:element name="Choices" type="Choices" minOccurs="1" maxOccurs="1"/>
   </xs:complexContent>
</xs:complexType>

<xs:complexType name="Choices">
   <xs:sequence>
       <xs:element name="Choice" type="Choice" maxOccurs="3" minOccurs="1"/>
       <xs:element name="CorrectChoice" type="CorrectChoice" maxOccurs="1" minOccurs="1"/>
   </xs:sequence>
</xs:complexType>

<xs:complexType name="Choice">
   <xs:simpleContent>
       <xs:extension base="xs:string"/>
   </xs:simpleContent>
</xs:complexType>

<xs:complexType name="CorrectChoice">
   <xs:simpleContent>
       <xs:extension base="xs:string"/>
           <xs:attribute name="correct" type="xs:boolean" default="false" use="required"/>
       </xs:extension>
   </xs:simpleContent>
</xs:complexType>

否则,如果您正在使用 XML 1.1 并希望单独保留元素 Choice,那么您可以通过使用一个断言来解决它,该断言会检查并计算您使用属性 [= 的频率13=]。这样的事情应该有效:

<xs:assert test="count(Choices//@correct) le 1"/>

此断言会将可在一个元素 Choices 中应用的属性 correct 的数量限制为 1。因此任何元素 Choices 多于一个 Choice具有正确属性的将失败。