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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With