Kypaz
Kypaz

Reputation: 411

Need any element among unordered children

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 ?

Upvotes: 0

Views: 73

Answers (2)

kjhughes
kjhughes

Reputation: 111621

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.

Upvotes: 1

James Wierzba
James Wierzba

Reputation: 17548

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.

Upvotes: 0

Related Questions