简体   繁体   English

用于重复元素序列的Xml Schema

[英]Xml Schema for repeating sequence of elements

I have xml as follows 我有xml如下

<Search>
    <Term />
    <And />
    <Term />
    <And />
    <Term />
</Search>

There can be n number of Terms and n-1 Ands (n > 0) in the sequence as shown. 如图所示,序列中可以有n个术语和n-1个And(n> 0)。 I tried the following xml schema but above xml would not get validated against the schema. 我尝试了以下xml架构,但上面的xml不会针对架构进行验证。 Error: cvc-complex-type.2.4.b: The content of element 'Search' is not complete. 错误:cvc-complex-type.2.4.b:元素“搜索”的内容不完整。 One of '{And}' is expected. 其中一个'{And}'是预料之中的。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="Search">
        <xs:complexType>
            <xs:sequence>               
                <xs:sequence minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="Term" type="xs:string" />
                    <xs:element name="And" type="xs:string" />
                </xs:sequence>              
                <xs:element name="Term" minOccurs="1" maxOccurs="1" type="xs:string" />             
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Appreciate any help with the xml schema. 感谢xml架构的任何帮助。

Reordering them like this seems to do it. 像这样重新排序他们似乎做到了。 Am I missing anything? 我错过了什么吗?

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="Search">
        <xs:complexType>
            <xs:sequence>
              <xs:element name="Term" minOccurs="1" maxOccurs="1" type="xs:string" />
                <xs:sequence minOccurs="0" maxOccurs="unbounded">
                  <xs:element name="And" type="xs:string" />
                  <xs:element name="Term" type="xs:string" />
                </xs:sequence>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The revised form of the content model will indeed recognize the language described. 内容模型的修订形式确实会识别所描述的语言。

But your XML might be a bit more idiomatic, and would almost certainly be easier to process, if you thought of the XML in terms of the abstract syntax tree you want, rather than in terms of a literal transcription of a surface syntax designed for sequences of tokens rather than trees. 但是,如果您根据所需的抽象语法树考虑XML,而不是根据为序列设计的表面语法的字面转录,那么您的XML可能会更加惯用,并且几乎肯定会更容易处理代币而不是树木。

Instead of using an empty And element between terms, wrap the conjunction of terms in an And element. 代替使用一个空的And元件术语之间,包裹术语在会合And元件。

<Search>
  <And>
    <Term>...</Term>
    <Term>...</Term>
    <Term>...</Term>
  </And>
</Search>

It's now trivially easy to do arbitrary Boolean combinations, without having to worry about what precedence order to ascribe to the operators: 现在很容易做任意的布尔组合,而不必担心将什么优先顺序归于运算符:

<Search>
  <Or>
    <And>
      <Term>...</Term>
      <Or>
        <Term>...</Term>
        <Term>...</Term>
      </Or>
    </And>
    <And>
       <Term>...</Term>
       <not><Term>...</Term></not>
    </And>
  </Or>
</Search>

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

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