在 XSD 中定义混合元素允许“嵌套" [英] Defining mixed element in XSD allowing “nesting”

查看:37
本文介绍了在 XSD 中定义混合元素允许“嵌套"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临以下问题.我需要为我的 XML 文件创建一个 XSD 架构.假设我有几个对话对象"节点:

I am facing the following problem. I need to create an XSD schema of my XML file. Let's say that I have several "Conversation Objects" nodes:

  • 留言
  • 结束
  • YesOrNoAnswer

哪些是我能够在 XSD 中描述的简单元素(或几乎).例如我的 End 节点是这样定义的:

which are simple elements (or almost) that I am able to describe in my XSD. For example my End node is defined in this way:

<xs:element name="END" type="EndType"/>

<xs:complexType name="EndType" />
    <xs:attribute name="completedMission" type="xs:string"/>
    <xs:attribute name="retry" type="xs:boolean"/>
</xs:complexType>

然后我有一个特别的名字:

Then I have a special one called:

  • 多选

这种元素的结构如下:

<multipleChoice actor="" numberOfChoices="" percentage=""> message text

    <choice>
        <effects name="" bar="" points="" action="" likelihood="" />
        ...
    </choice>

    <choice>
       <effects name="" bar="" points="" action="" likelihood="" />
       ...
    </choice>

    <choice>
       <effects name="" bar="" points="" action="" likelihood="" />
       ...
    </choice>

</multipleChoice>

所以,第一个问题是:

  • 必须与numberOfChoices"属性中定义的选择"节点数量相同
  • 选择"节点必须跟在效果"节点之后,它只包含一些属性
  • 在效果"节点之后可以插入任何其他类型的节点,没有任何顺序,可以插入任意次数.例如,任何简单的节点(消息、结束、YesOrNoAnswer)或multipleChoice"节点,允许在我们想要的任意深度嵌套.

如何在 XSD 中定义multipleChoice"节点(包括属性、文本、选择"和效果"节点)?提前致谢!

How can I define the "multipleChoice" node in XSD (including attributes, text, "choice" and "effect" nodes) ? Thanks in advance!

推荐答案

这个答案怎么样 - 它只适用于 XSD 1.1!根据@Tarta 的反馈更新.

How about this answer -it works only in XSD 1.1! Updated based on feedback from @Tarta.

  <xs:element name="multipleChoice">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" ref="choice"/>
      </xs:sequence>
      <xs:attribute name="actor"/>
      <xs:attribute name="numberOfChoices"/>
      <xs:attribute name="percentage"/>
      <xs:assert test="count(./choice) = @numberOfChoices"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="choice">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="1" ref="effects"/>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element minOccurs="0" maxOccurs="unbounded" ref="Message"/>
          <xs:element minOccurs="0" maxOccurs="unbounded" ref="End"/>
          <xs:element minOccurs="0" maxOccurs="unbounded" ref="YesOrNoAnswer"/>
          <xs:element minOccurs="0" maxOccurs="unbounded" ref="multipleChoice"/>          
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="effects">
    <xs:complexType>
      <xs:attribute name="name"/>
      <xs:attribute name="bar"/>
      <xs:attribute name="points"/>
      <xs:attribute name="action"/>
      <xs:attribute name="likelihood"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="Message">
    <xs:simpleType>
      <xs:restriction base="xs:string"/>
    </xs:simpleType>
  </xs:element>
  <xs:element name="End">
    <xs:simpleType>
      <xs:restriction base="xs:string"/>
    </xs:simpleType>
  </xs:element>
  <xs:element name="YesOrNoAnswer">
    <xs:simpleType>
      <xs:restriction base="xs:string"/>
    </xs:simpleType>
  </xs:element>

这应该为每个 multipleChoice 提供正确数量的选择元素,使用断言来强制执行.它还允许嵌套元素.

That should give the correct number of choice elements for each multipleChoice, using the assert to enforce that. It also allows the nesting of elements.

这个版本现在刚好从 1 个效果元素开始,然后按任意顺序添加 0 个或多个其他元素.

This version now starting with exactly 1 effects element and then 0 or more of the other elements in any order.

这篇关于在 XSD 中定义混合元素允许“嵌套"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆