0

I need to verify the presence a list of children of an XML element, but I also need to allow one of the child elements to be any other element.

For example, if the XML is like this :

<fruits>
  <item1>banana</item1>
  <item2>apple </item2>
  <anything>yolo</anything>
</fruits>

And with an XSD like this :

<xsd:complexType name="fruits">
  <xsd:all>
    <xsd:element name="item1" type="xsd:string" minOccurs="1" maxOccurs="1" />
    <xsd:element name="item2" type="xsd:string"  minOccurs="1" maxOccurs="1" />
  </xsd:all>
</xsd:complexType>

I would like this xml file to be ok at the verification. But with my xsd file, i get an error like this :

The element 'fruits' has invalid child element 'anything'.

Do you have any advice ?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Kypaz
  • 411
  • 3
  • 11

2 Answers2

1

You can give up the unordered requirement and use xs:any in an xs:sequence, or you can meet the unordered requirement and use a fixed wrapper element around your xs:any element in an xs:all.

You cannot have it both ways. XSD is not as orthogonal as your expectations.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Yep, I saw that on this topic : http://stackoverflow.com/questions/3347822/validating-xml-with-xsds-but-still-allow-extensibility. I will find another way. thanks – Kypaz Jul 02 '15 at 15:11
0

Try using the any element .

The any element enables the author to extend the XML document with elements not specified by the schema.

According to the documentation you will need to use sequence instead of all, because the only valid parent elements of any are choice and sequence.

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • Yes, i tried that, but as you said, element are not allowed in element. And I need to keep the , because my child element can appear in any order ... – Kypaz Jul 02 '15 at 14:07
  • I suppose you could place the any element with minOccurs=0 in between each of the required elements ? This would effectively allow any ordering – James Wierzba Jul 02 '15 at 14:14