I’m interested in XML files with structure:
<resource>
<type>STRING</type>
<metadata>
<ANY_EXTERNAL_ELEMENT1>
<value>STRING</value>
</ANY_EXTERNAL_ELEMENT1>
<ANY_EXTERNAL_ELEMENT2>
<reference>STRING</reference>
</ANY_EXTERNAL_ELEMENT2>
<ANY_EXTERNAL_ELEMENT3>
<value>STRING</value>
</ANY_EXTERNAL_ELEMENT3>
</metadata>
</resource>
metadata element need to have at least one ANY_EXTERNAL_ELEMENT child that need to have only one child element with name in set {“reference”, “value”}.
Is it possible to achieve it in XMLSchema?
What I tried:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="resource">
<xs:complexType>
<xs:all>
<xs:element name="type" type="xs:string"/>
<xs:element name="metadata">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="1">
<xs:complexType>
<xs:choice>
<xs:element name="reference"/>
<xs:element name="value"/>
</xs:choice>
</xs:complexType>
</xs:any>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
But it’s not valid.
I need help.
Thanks in advance.
No, you cannot constrain an “external” element like that. The content model for <any> allows only <annotation>. Wouldn’t it be more natural to kind of invert the structure:
But, of course, I have no idea what your use case is.