[英]Is it possible in W3C's XML Schema language (XSD) to allow a series of elements to be in any order but still limit occurrences?
I know about all
and choice
, but they don't account for a case where I do want some elements to be able to occur more than once, such as: 我了解
all
和choice
,但是它们并不能说明我确实希望某些元素能够多次出现的情况,例如:
<Root>
<ThingA/>
<ThingB/>
<ThingC/>
<ThingC/>
<ThingC/>
</Root>
I could use sequence
, but I'd prefer to allow these children to be in any order. 我可以使用
sequence
,但是我更希望允许这些子元素以任意顺序排列。 I could use any
, but then I couldn't have more than one ThingC
. 我可以用
any
,但是之后我不能有一个以上的ThingC
。 I could use choice
, but then I couldn't limit ThingA
and ThingB
to 0 or 1. 我可以使用
choice
,但是不能将ThingA
和ThingB
限制为0或1。
I think I may have read somewhere that this was either difficult or impossible in XSD, but might be possible with RELAX NG. 我想我可能在某处已经读到这在XSD中是困难的还是不可能的,但是使用RELAX NG可能是可能的。 I don't remember where I read that, unfortunately.
不幸的是,我不记得我从哪里读到的。
Thanks for any help! 谢谢你的帮助!
That's right: you can't do what you want to do in XML Schema, but you can in RELAX NG with: 没错:您无法在XML Schema中完成您想做的事情,但是您可以在RELAX NG中执行以下操作:
<element name="Root">
<interleave>
<element name="ThingA"><empty /></element>
<element name="ThingB"><empty /></element>
<oneOrMore><element name="ThingC"><empty /></element></oneOrMore>
</interleave>
</element>
Your options in XML Schema are: 您在XML模式中的选项是:
<xs:sequence>
<xs:sequence>
<xs:choice>
, and add extra validation (for example using Schematron) to check that there's not more than one <ThingA>
or <ThingB>
<xs:choice>
,并添加额外的验证(例如使用Schematron)以检查是否有不超过一个<ThingA>
或<ThingB>
It turns out that the third is usually the best option; 事实证明,第三个通常是最好的选择。 there's usually not much cost for generators of XML to output elements in a particular order, and not only does it help validation but it also aids consumption of the XML if the order can be known in advance.
XML生成器以特定顺序输出元素通常没有多少成本,而且如果可以提前知道顺序,它不仅有助于验证,而且还有助于XML的使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.