Question

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.

Was it helpful?

Solution

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:

<resource>
<type>STRING</type>
<metadata>
    <external value="STRING">
        <ANY-EXTERNAL-ELEMENT-1/>
    </external>
    <external ref="STRING">
        <ANY-EXTERNAL-ELEMENT-2/>
    </external>
</metadata>
</resource>

But, of course, I have no idea what your use case is.

OTHER TIPS

I think you have to enumerate the possible elements (ANY-EXTERNAL-ELEMENT-1 etc) either in an xs:choice or by means of a substitution group if you want to contain their type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top