简体   繁体   English

使用XSD进行XML验证:如何避免关注元素序列?

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

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. 这里的问题是:元素位置,multipleChoiceInput等必须以它们声明的相同顺序出现。 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? 我怎样才能做到这一点?

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. 但我仍然遇到min-和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. 在这个例子中,我可以拥有尽可能多的“pictureInput”,但是我希望拥有0或1的约束。

Thanks a lot for helping! 非常感谢您的帮助!

<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

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". 关键是将xs:choice与maxOccurs =“unbounded”结合起来。 If you just use xs:all, you are allowed one of each, period. 如果您只使用xs:all,则允许每个句点中的一个。

edited to add: While xs:any will work, it won't limit your choices to the four elements itemized. 编辑添加:虽然xs:any将起作用,但它不会将您的选择限制为逐项列出的四个元素。 It will allow anything, which pretty much defeats the purpose of a schema. 它将允许任何东西,这几乎违背了模式的目的。

Also very late to the party here, but would using <xsd:all> in conjunction with minOccurs and maxOccurs not work?: 这里聚会也很晚,但是将<xsd:all>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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM