Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml Schema for repeating sequence of elements

Tags:

xml

xsd

I have xml as follows

<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. I tried the following xml schema but above xml would not get validated against the schema. Error: cvc-complex-type.2.4.b: The content of element 'Search' is not complete. One of '{And}' is expected.

<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.

like image 555
bluetech Avatar asked Dec 12 '22 01:12

bluetech


2 Answers

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>
like image 85
user845279 Avatar answered Dec 26 '22 06:12

user845279


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.

Instead of using an empty And element between terms, wrap the conjunction of terms in an And element.

<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>
like image 21
C. M. Sperberg-McQueen Avatar answered Dec 26 '22 08:12

C. M. Sperberg-McQueen