使用 XSD 进行 XML 验证:如何避免关心元素的顺序?
我有以下 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 的限制相反。
非常感谢您的帮助!
I have following XSD code:
<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?
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.
Thanks a lot for helping!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:我已将“序列”更改为“所有”
序列强制顺序(按定义)。如果顺序无关紧要,则全部使用。
如果元素出现多次的机会,则可以使用 xsd:any。
您可以在以下链接找到 xsd:any 的详细信息:
https://www.w3schools.com/xml /schema_complex_any.asp
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.
You can find details of xsd:any at following link:
https://www.w3schools.com/xml/schema_complex_any.asp
我对这个讨论有点晚了,但我遇到了同样的问题并找到了解决方案:
关键是将 xs:choice 与 maxOccurs="unbounded" 结合起来。如果您只使用 xs:all,则允许您分别使用一个,句号。
编辑添加:
虽然 xs:any 可以工作,但它不会将您的选择限制为逐项列出的四个元素。它允许任何事情,这几乎违背了模式的目的。
I'm a little late to this discussion, but I had the same problem and found the solution:
The key is to combine xs:choice with maxOccurs="unbounded". If you just use xs:all, you are allowed one of each, period.
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.
这里的聚会也很晚了,但是将
与minOccurs
和maxOccurs
结合使用不起作用吗?:Also very late to the party here, but would using
<xsd:all>
in conjunction withminOccurs
andmaxOccurs
not work?: