如何使用出现约束为无序的XML节点列表创建模式

Mar*_*k H 17 xml xsd

给定像这样的XML布局,我正在尝试创建一个XSD架构来验证它.

<RootNode>
  <ChildA />
  <ChildC />
  <ChildB />
  <ChildB />
  <ChildA />
</RootNode>
Run Code Online (Sandbox Code Playgroud)

要求如下:

  • ChildA,ChildB和ChildC可以按任何顺序出现.(<xs:sequence>不适合)
  • ChildA是强制性的,但可能会多次出现.
  • ChildB是可选的,可能会多次出现.
  • ChildC是可选的,可能发生一次.

我通常用来创建无序节点列表的技术是使用a <xs:choice maxOccurs="unbounded">与列表中的每个可能节点,但是,我无法minOccurs="1"在ChildA上创建约束和ChildC maxOccurs="1"上的约束.(选择的出现次数优先于此处的元素).

<xs:element name="RootNode">
  <xs:complexType>
    <xs:choice minOccurs="1" maxOccurs="unbounded">
      <xs:element name="ChildA" minOccurs="1"/>
      <xs:element name="ChildB" />
      <xs:element name="ChildC" maxOccurs="1"/>
    </xs:choice>
  </xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

jas*_*sso 11

更新:在XSD 1.1m中,对all-groups的一些约束已被解除.在这里这里看到答案.

不是一个简单的,但似乎可行.这里的困难部分是模式定义必须是确定性的.我使用的方法是通过绘制它的有限状态自动机来可视化问题,然后编写与自动机相对应的正则表达式.它听起来并不复杂.尽管如此,使用其他一些验证系统可能会提供更简单的答案.

我做了一些测试,但遗漏了一些特殊情况很容易.如果您发现错误,请发表评论.

......这是代码:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

    <!-- Schema for elements ChildA, ChildB and ChildC
        The requirements are as follows:
            * ChildA, ChildB and ChildC may occur in any order.
            * ChildA is mandatory but may occur multiple times.
            * ChildB is optional and may occur multiple times.
            * ChildC is optional and may occur once only.
    -->

    <xsd:element name="root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="ABC-container" type="ABC" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="ABC">
        <xsd:sequence>
            <xsd:element name="ChildB" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:choice>
                <xsd:sequence maxOccurs="1">
                    <xsd:element name="ChildC" type="xsd:string"/>
                    <xsd:element name="ChildB" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
                    <xsd:element name="ChildA" type="xsd:string"/>
                    <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                        <xsd:element name="ChildA" type="xsd:string" minOccurs="0"/>
                        <xsd:element name="ChildB" type="xsd:string" minOccurs="0"/>
                    </xsd:sequence>
                </xsd:sequence>
                <xsd:sequence maxOccurs="1">
                    <xsd:element name="ChildA" type="xsd:string" minOccurs="1"/>
                    <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                        <xsd:element name="ChildA" type="xsd:string" minOccurs="0"/>
                        <xsd:element name="ChildB" type="xsd:string" minOccurs="0"/>
                    </xsd:sequence>
                    <xsd:sequence minOccurs="0" maxOccurs="1">
                        <xsd:element name="ChildC" type="xsd:string"/>
                        <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                            <xsd:element name="ChildA" type="xsd:string" minOccurs="0"/>
                            <xsd:element name="ChildB" type="xsd:string" minOccurs="0"/>
                        </xsd:sequence>
                    </xsd:sequence>
                </xsd:sequence>
            </xsd:choice>
        </xsd:sequence>
    </xsd:complexType>

</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

  • "通过组合和嵌套XML Schema提供的各种组,并通过设置`minOccurs和maxOccurs`的值,可以表示任何可用XML 1.0 DTD表达的内容模型." 但他们从未说过会很漂亮. (6认同)