【问题标题】:XML validation with XSD: how to avoid caring about the sequence of the elements?使用 XSD 进行 XML 验证:如何避免关心元素的顺序?
【发布时间】:2010-07-24 13:23:40
【问题描述】:

我有以下 XSD 代码:

<xsd:complexType name="questions">
    <xsd:sequence>
        <xsd:element name="location" type="location"/>
        <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="pictureInput" type="pictureInput" minOccurs="0"/>
    </xsd:sequence>
</xsd:complexType>

这里的问题是:元素位置、multipleChoiceInput 等必须按照它们声明的顺序出现。我不希望这种情况发生,我希望在验证过程中,序列不应该是相关的。我怎样才能做到这一点?

我尝试过的另一种可能性是:

<xsd:complexType name="questions">

        <xsd:choice maxOccurs="unbounded">   
            <xsd:element name="location" type="location"/>  
            <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
        </xsd:choice>            

</xsd:complexType>

在这个例子中,顺序真的不再重要了,我可以有很多我想要的元素(“全部”不允许我这样做)。但我仍然有 min- 和 maxOccurs 的问题。在这个例子中,我可以有尽可能多的“pictureInput”,而我希望有 0 或 1 的约束条件。

非常感谢您的帮助!

【问题讨论】:

    标签: xml xsd xml-validation


    【解决方案1】:
    <xsd:complexType name="questions">
        <xsd:all>
            <xsd:element name="location" type="location"/>
            <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
            <xsd:element name="textInput" type="textInput"/>
            <xsd:element name="pictureInput" type="pictureInput"/>
        </xsd:all>
    </xsd:complexType>
    

    注意:我已将“序列”更改为“全部”

    序列强制顺序(定义)。如果顺序无关紧要,则全部使用。

    如果元素有多次出现的机会,则可以使用 xsd:any。

    <xsd:complexType name="questions">
        <xsd:sequence>
            <xsd:any minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
    

    您可以在以下链接中找到 xsd:any 的详细信息:

    https://www.w3schools.com/xml/schema_complex_any.asp

    【讨论】:

    • 感谢您回答 YoK,但在我的情况下不能使用“all”,因为“all”要求元素仅出现一次(min- 和 maxOccurs 只能接受值 0 和 1) .
    • 那么,也许&lt;xs:any&gt; 是你的朋友。
    • 是的,在这种情况下需要使用任何东西。也会更新答案。
    • 再次感谢大家,但“ANY”和“ALL”都没有考虑以下因素:1)我想要一个,而且只有一个元素“位置”2)我想要 0或 1 个元素“pictureInput”...期待其他建议。
    • 您确认“ALL”不起作用了吗?因为在下面的链接上它说:他所有元素都提供了一个 XML 表示,它描述了一组无序的元素类型。对于与 XML Schema Document 中的 all 元素关联的每种元素类型,对应的 XML 实例中必须有对应的元素。但是,它们可能以任何顺序出现。事实上,每种类型可能有零个或多个元素,具体取决于与相应元素类型关联的 minOccurs 和 maxOccurs 属性的值。 xmlschemareference.com/allElement.html
    【解决方案2】:

    这个讨论我有点晚了,但我遇到了同样的问题并找到了解决方案:

    <xsd:complexType name="questions">
        <xsd:choice maxOccurs="unbounded">
            <xsd:element name="location" type="location"/>
            <xsd:element name="multipleChoiceInput" type="multipleChoiceInput"/>
            <xsd:element name="textInput" type="textInput"/>
            <xsd:element name="pictureInput" type="pictureInput"/>
        </xsd:choice>
    </xsd:complexType>
    

    关键是将 xs:choice 与 maxOccurs="unbounded" 结合起来。如果您只使用 xs:all,您可以使用每个句号。

    编辑添加: 虽然 xs:any 会起作用,但它不会将您的选择限制在逐项列出的四个元素中。它将允许任何事情,这几乎违背了模式的目的。

    【讨论】:

    • 对我来说,这是解决此类问题的最佳方法,尽管它并不完美。在这种情况下,这不符合具有 0 或 1 个“pictureInput”的要求。您可以添加超过 1 个并且设置 maxOccurs 不能阻止这种情况(因为选择本身是未绑定的)。
    【解决方案3】:

    在这里聚会也很晚,但将&lt;xsd:all&gt;minOccursmaxOccurs 结合使用不起作用?:

    <xsd:complexType name="questions">
        <xsd:all>
            <xsd:element name="location" type="location" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="1"/>
            <xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
        </xsd:all>
    </xsd:complexType>
    

    【讨论】:

    • 不,因为在 any 里面你不能定义 maxOccurs 大于 1
    • @sotix:我不明白你的评论:答案使用all 而不是any,而且are 没有maxOccurs &gt; 1。那么使用all 真的不能工作吗?
    • @MarcusMangelsdorf 已经有一段时间了,所以我只能重新阅读。从接受的答案来看all 不能包含maxOccurs &gt; 1,这是原始问题的一部分。所以我想我在之前的评论中混淆了allany
    • 你说得对,它不适用于原始问题,而且肯定是错字。但是@gaelicyoda 的代码示例仍然正确且有效(这正是我所需要的),尽管maxOccurs="1" 是多余的,因为the <all> element defaults to minOccurs="1" and maxOccurs="1"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2018-02-09
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    相关资源
    最近更新 更多