0

I have a problem, I can't seem to find solution to, using only XML 1.0. My task is to create schema that will validate xml element differently based on a value of one of it's attributes. Here is simplified example:

<elements>
    <elem type="A">
        <foo/>
    </elem>
    <elem type="B">
        <goo/>
    </elem>
</elements>

So I would like to check that all elem's of type="A" can only have child element foo and all elem's of type="B" can only have child element goo. What I was able to create so far was this schema.

<xs:element name="elements">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="elem" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="elem">
    <xs:complexType>
        <xs:choice>
            <xs:element ref="foo"/>
            <xs:element ref="goo"/>
        </xs:choice>
        <xs:attribute name="type" type="elem-type" use="required">
    </xs:complexType>
</xs:element>
<xs:simpleType name="elem-type">
    <xs:restriction base="xs:token">
        <xs:enumeration value="A"/>
        <xs:enumeration value="B"/>
    </xs:restriction>
</xs:simpleType>
Maroš Beťko
  • 2,181
  • 2
  • 16
  • 42

0 Answers0