简体   繁体   English

XML Schema可以指定共现约束吗?

[英]Can XML Schema specify co-occurrence constraints?

Can an XML Schema document specify that two items must co-occur? XML Schema文档是否可以指定必须同时出现两个项目?

For example, that there are two optional elements, and they are either both present or both absent. 例如,有两个可选元素,它们要么都存在,要么都不存在。

a b? c d? e      # giving only {ace, abcde}
                 # instead of all combinations: {ace, acde, abce, abcde}

<element name="root">
  <complexType>
    <sequence>
      <element ref="a"/>
      <element ref="b" minOccurs="0"/>
      <element ref="c"/>
      <element ref="d" minOccurs="0"/>
      <element ref="e"/>
    <sequence>
  <complexType>
<element>

Another example: that there are two repeated elements, and that however many times the first one occurs, the second also occurs that many times: 另一个例子:有两个重复的元素,并且不管第一个元素发生多少次,第二个元素也发生多次:

a b^n c d^n e  # where ^n is a superscript denoting number of repeats
               # giving {ace, abcde, abbcdde, abbbcddde, ...}
               # but no other combinations

<element name="root">
  <complexType>
    <sequence>
      <element ref="a"/>
      <element ref="b" minOccurs="0" maxOccurs="unbounded"/>
      <element ref="c"/>
      <element ref="d" minOccurs="0" maxOccurs="unbounded"/>
      <element ref="e"/>
    <sequence>
  <complexType>
<element>

Maybe there's something in the identity constraints in the XML Schema spec, but that seems to be about getting exactly one instance with a certain characteristic, rather than ensuring two have the same characteristic. XML Schema规范中的身份约束中可能有某些内容,但这似乎是要获取具有特定特征的一个实例,而不是确保两个实例具有相同的特征。

Co-occurrence is one of the things that the existing 1.0 Schema spec can't address. 共存是现有的1.0 Schema规范无法解决的问题之一。 This is one of the reasons why Schematron was released. 这是Schematron发布的原因之一。 Assertion based validation can handle this case, along with any other that can be expressed via an XPath express rather easily. 基于断言的验证可以处理这种情况,以及可以通过XPath Express表达的其他任何情况,都非常容易。

Furthermore, in the 1.1 Schema spec there is assertion functionality, but I'm not aware of widespread processor support for 1.1 yet. 此外,在1.1 Schema 规范中有断言功能,但是我还不知道处理器对1.1的广泛支持。

The classic example that assertion based validation usually revolves around a credit card transaction, for example: 基于断言的验证的典型示例通常围绕信用卡交易,例如:

<card>
  <number>1111-1111-1111</number>
  <type>mastercard</type>
</card>

Here we want to make sure that mastercard numbers start with '1' and visa starts with '2' (not the real convention, of course). 在这里,我们要确保万事达卡号码以“ 1”开头,签证以“ 2”开头(当然不是真正的惯例)。 There's no way to do that with Schema 1.0, but rather easy via an assertion (in pseudo-code) 使用Schema 1.0无法做到这一点,但是通过断言(用伪代码)相当容易

<assert test="starts-with(card/type[.='mastercard'],'1')"/>

Not sure about the ability to do it directly. 不确定直接执行此操作的能力。 A simple option though would be to embed them in a single optional element and have each element of the new element be required. 不过,一个简单的选择是将它们嵌入单个可选元素中,并要求新元素中的每个元素。 Something along the lines of: 类似于以下内容:

<element name="root">
  <complexType>
    <sequence>
      <element ref="a"/>
      <element ref="c"/>
      <element ref="f" minOccurs="0">
        <complexType>
          <element ref="b" minOccurs="1"/>
          <element ref="d" minOccurs="1"/>
        </complexType>
      </element>
      <element ref="e"/>
    </sequence>
  </complexType>
</element>

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

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