Xml scheme for repeating a sequence of elements

I have xml as follows

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

A sequence may contain n number of terms and n-1 Ands (n> 0). I tried the following xml scheme, but above xml did not receive confirmation from the scheme. Error: cvc-complex-type.2.4.b: The content of the Search element 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 xml schema.

+4
source share
2 answers

Reordering them like this seems to do it. Did I miss something?

 <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> 
+9
source

The revised form of the content model does recognize the described language.

But your XML might be a little more idiomatic and would almost certainly be easier to handle if you thought of XML in terms of the abstract syntax tree you want, and not in terms of literal transcription of surface syntax intended for sequences of tokens, not trees.

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

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

It is now trivially easy to execute arbitrary Boolean combinations without worrying about what order of priorities is assigned 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> 
+4
source

All Articles