首页 > 解决方案 > 这是对 xsd:choice 的错误使用吗?

问题描述

我正在用 JavaScript 编写一个程序,它读取 XSD、创建 HTML 表单并将表单导出到 XML。

为了做到这一点,我必须学习 XSD 的结构,但我对xsd:choice.

在给我的示例 XSD 文件中,我有这个:

<xsd:choice>
    <xsd:sequence>
        <xsd:element name="tagname1" type="pedidoInf:complexTypeName" />
    </xsd:sequence>
    <xsd:sequence>
        <xsd:choice>
            <xsd:sequence>
                <xsd:element name="tagname2" type="pedidoInf:otherComplexTypeName" />
            </xsd:sequence>
            <xsd:sequence>
                <xsd:element name="tagname3" type="pedidoInf:anotherComplexTypeName" />
            </xsd:sequence>
        </xsd:choice>
    </xsd:sequence>
</xsd:choice>

我不知道是我理解不xsd:choice正确还是这段代码xsd:choice无缘无故地使用了嵌套。

上面的代码不会与以下代码完全相同:

<xsd:choice>
    <xsd:sequence>
        <xsd:element name="tagname1" type="pedidoInf:complexTypeName" />
    </xsd:sequence>
    <xsd:sequence>
        <xsd:element name="tagname2" type="pedidoInf:otherComplexTypeName" />
    </xsd:sequence>
    <xsd:sequence>
        <xsd:element name="tagname3" type="pedidoInf:anotherComplexTypeName" />
    </xsd:sequence>
</xsd:choice>

标签: xmlxsdxsd-validationxml-validation

解决方案


标题问题:不,两种用法xsd:choice都不正确——它们只是比它们必须的更复杂。

(而且,是的,这两个xsd:choice结构是相同的。)

两者也等同于:

  <xsd:choice>
    <xsd:element name="tagname1" type="pedidoInf:complexTypeName" />
    <xsd:element name="tagname2" type="pedidoInf:otherComplexTypeName" />
    <xsd:element name="tagname3" type="pedidoInf:anotherComplexTypeName" />
  </xsd:choice>

推荐阅读