【问题标题】:How to design an xsd for multiple choice questions?如何为多项选择题设计xsd?
【发布时间】:2021-10-14 01:32:43
【问题描述】:

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

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

注意葡萄的正确属性。使用它是为了当我在 UI 中渲染它时,它旁边有一个绿色复选标记。但是,对于我当前的 xsd,我无法阻止用户根本不放置任何正确的="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>

【问题讨论】:

  • 我很想说所有足够有趣的问题都有不止一个正确答案...

标签: xml xsd schema processor multiple-choice


【解决方案1】:

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

<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,那么您可以通过使用断言来检查和计算您使用属性 correct 的频率来解决它。这样的事情应该可以工作:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多