使用XSD进行XML验证:如何避免关心元素的顺序?

使用XSD进行XML验证:如何避免关心元素的顺序?,xml,xsd,xml-validation,Xml,Xsd,Xml Validation,我有以下XSD代码: <xsd:complexType name="questions"> <xsd:sequence> <xsd:element name="location" type="location"/> <xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded

我有以下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>

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

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

<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

非常感谢你的帮助


<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的详细信息:


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

<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可以工作,但它不会将您的选择限制在列出的四个元素上。它将允许几乎违背模式目的的任何事情。

这里的聚会也很晚了,但是将
minOccurs
maxOccurs
结合使用将不起作用:

<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>


谢谢你回答YoK,但是“all”不能用在我的例子中,因为“all”要求元素只出现一次(min-和maxOccurs只能接受值0和1)。那么,也许
是你的朋友。在这种情况下,你需要使用任何元素。也将更新答案。再次感谢各位,但无论是“任何”还是“所有”都不会考虑以下内容:1)我想要一个且只有一个元素“位置”2)我想要0或1个元素“图片输入”。。。期待其他建议。您是否验证“全部”不起作用?因为在下面的链接中它说:他所有的元素都提供了一个XML表示,它描述了一组无序的元素类型。对于与XML模式文档中的all元素关联的每个元素类型,相应的XML实例中必须有相应的元素。但是,它们可能以任何顺序出现。事实上,根据与相应元素类型关联的minOccurs和maxOccurs属性的值,每个类型可能有零个或多个元素。对我来说,这是解决此类问题的最佳方法,尽管它并不完美。在这种情况下,这不符合0或1“pictureInput”的要求。您可以添加多个值,而设置maxOccurs无法阻止此操作(因为选项本身未绑定)。否,因为在任何值中,您不能定义maxOccurs大于1@sotix:我不理解你的评论:答案使用了
all
而不是
any
,而且没有
maxOccurs>1
。那么,使用
all
,它真的能工作吗?@MarcusMangelsdorf它已经有一段时间了,所以我只能重新阅读。根据接受的答案判断,所有都不能包含
maxOccurs>1
,这是原始问题的一部分。所以我猜我在前面的评论中把
所有的
任何的
都弄混了。你完全正确,它对原来的问题不起作用,而且肯定是个打字错误。但是@gaelicyoda的代码示例仍然正确且有效(这正是我所需要的),尽管
maxOccurs=“1”
是多余的,因为。