How to declare a custom XML Schema Example (XSD)

I need your help to identify a special case in an XML schema: A sequence that contains two elements: 'x' and 'y', whereas:

  • <x> element can appear 0 until unrelated times in sequence

  • <y> element can be displayed 0 to 1 times in a sequence

  • <x> and <y> locations can be anywhere - that is, it is possible to have unrelated times an <x> element, then one <y> element, and then an unbound times <x> element.

XML examples of this problem:

Example # 1

 <x>stuff</x> <y>stuff</y> <x>stuff</x> 

Example # 2

 <y>stuff</y> <x>stuff</x> <x>stuff</x> 

Example # 3

 <x>stuff</x> <x>stuff</x> <y>stuff</y> <x>stuff</x> 

Thanks!

+4
source share
4 answers

For various reasons, none of the samples from Yuval, Mo, or Davidldon work. Here is the one that does.

  <xs:complexType name="myComplexType"> <xs:sequence> <xs:element name="x" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> <xs:sequence minOccurs="0"> <xs:element name="y" type="xs:string"/> <xs:element name="x" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:sequence> </xs:complexType> 
+5
source

EDIT: As Alochchi said, my decision is wrong. According to the specification, an element in xs: all can appear only zero or once. Sorry for the inconvenience

I think what you want is not consistency. A sequence defines not only elements, but also order. And in your case, the order may change. Have you tried xs: everything?

 <xs:complexType name="myComplexType"> <xs:all> <xs:element name="x" type="xs:string" maxOccurs="unbounded"/> <xs:element name="y" type="xs:string" maxOccurs="1"/> </xs:all> </xs:complexType> 

Another example might be to make it a sequence, but check the sequence maxOccurs="unbounded"

+2
source

Not too complicated. At the top of my head it should be something like this:

 <xs:element name="x" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> <xs:element name="y" type="xs:string" minOccurs="0" maxOccurs="1"/> <xs:element name="x" type="xs:string" minOccurs="0" maxOccurs="unbounded"/> 

Since each element in the XSD is optional by default, this XSD will correspond to the XML structure you defined, with the y element appearing somewhere before, after, or between the x elements, with a maximum value of 1

0
source

It has been a while since I used the circuit, but I think the sequence is your answer here.

You need to have an unlimited number of options between (x) or (ay followed by x).

 <xsd:element name="parent"> <xsd:complexType> <xsd:choice maxOccurs="unbounded"> <xsd:element name="x" type="xs:string"/> <xsd:sequence> <xsd:element name="y" type="xsd:string" /> <xsd:element name="x" type="xsd:string" /> </xsd:sequence> </xsd:choice> </xsd:complexType> </xsd:element> 
0
source

All Articles