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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3325247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:12:51  来源:igfitidea点击:

XML validation with XSD: how to avoid caring about the sequence of the elements?

xmlxsdxml-validation

提问by jcborges

I have following XSD code:

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

The problem here is: the elements location, multipleChoiceInput, etc. must appear in the same order they are declared. I don't want this to happen, I want that, in the validation process the sequence should not be relevant. How can I achieve this?

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

Another possibility I've tried has been:

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

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

In this example, the sequence really does not matter anymore, and I can have so much elements as I want (what "all" would not allow me to do). But I still have the Problem with the min- and maxOccurs. In this example, I could have so many "pictureInput"s as possible, what is againt the constraint that I would like to have either 0 or 1.

在这个例子中,顺序真的不再重要了,我可以拥有我想要的元素(“所有”不允许我做的事情)。但我仍然有 min- 和 maxOccurs 的问题。在这个例子中,我可以有尽可能多的“图片输入”,什么是我想要 0 或 1 的约束。

Thanks a lot for helping!

非常感谢您的帮助!

回答by YoK

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

NOTE: I have changed "sequence" to "all"

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

Sequence forces order (as defined). if order doesn't matter then all is used.

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

If there are chances of element occurence more than once then xsd:any can be used.

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

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

You can find details of xsd:any at following link:

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

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

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

回答by CD Jorgensen

I'm a little late to this discussion, but I had the same problem and found the solution:

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

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

The key is to combine xs:choice with maxOccurs="unbounded". If you just use xs:all, you are allowed one of each, period.

关键是将 xs:choice 与 maxOccurs="unbounded" 结合起来。如果您只使用 xs:all,则允许您使用每个句点之一。

edited to add: While xs:any will work, it won't limit your choices to the four elements itemized. It will allow anything, which pretty much defeats the purpose of a schema.

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

回答by gaelicyoda

Also very late to the party here, but would using <xsd:all>in conjunction with minOccursand maxOccursnot work?:

在这里参加聚会也很晚,但是会<xsd:all>与 with 一起使用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>