是否有可能在W3C的XML Schema语言(XSD)中允许一系列元素以任何顺序但仍然限制出现?

时间:2021-03-13 17:19:25

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:

我知道所有和选择,但他们没有考虑我希望某些元素能够多次出现的情况,例如:

<Root>
    <ThingA/>
    <ThingB/>
    <ThingC/>
    <ThingC/>
    <ThingC/>
</Root>

I could use sequence, but I'd prefer to allow these children to be in any order. I could use any, but then I couldn't have more than one ThingC. I could use choice, but then I couldn't limit ThingA and ThingB to 0 or 1.

我可以使用序列,但我更愿意让这些孩子处于任何顺序。我可以使用任何,但后来我不能有多个ThingC。我可以使用选择,但是我不能将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. I don't remember where I read that, unfortunately.

我想我可能已经读过某些地方,这在XSD中要么是困难的,要么是不可能的,但RELAX NG也许可能。不幸的是,我不记得我在哪里读到的。

Thanks for any help!

谢谢你的帮助!

1 个解决方案

#1


6  

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 Schema中的选项是:

  • add a preprocessing step that normalises your input XML into a particular order, and then use <xs:sequence>
  • 添加一个预处理步骤,将输入XML规范化为特定顺序,然后使用

  • use <xs:choice>, and add extra validation (for example using Schematron) to check that there's not more than one <ThingA> or <ThingB>
  • 使用 ,并添加额外的验证(例如使用Schematron)来检查是否存在多个

  • decide to fix the order of the elements in your markup language
  • 决定修复标记语言中元素的顺序

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。

#1


6  

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 Schema中的选项是:

  • add a preprocessing step that normalises your input XML into a particular order, and then use <xs:sequence>
  • 添加一个预处理步骤,将输入XML规范化为特定顺序,然后使用

  • use <xs:choice>, and add extra validation (for example using Schematron) to check that there's not more than one <ThingA> or <ThingB>
  • 使用 ,并添加额外的验证(例如使用Schematron)来检查是否存在多个

  • decide to fix the order of the elements in your markup language
  • 决定修复标记语言中元素的顺序

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。