Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD - Restricting attribute values to another element attribute value

Tags:

xsd

I have the following XML:

<Content name="contentName1">
    <!-- Some sub elements here -->
</Content>

<Sequence Name="sequenceName1">
    <Content name="contentName1" />
    <!-- Some sub elements here -->
</Sequence>

with the following XSD

<xs:element maxOccurs="unbounded" name="Content">
    <xs:complexType>
        <xs:attribute name="Name" type="xs:string" use="required" />
        <!-- other definitions here -->
     </xs:complexType>
</xs:element>

<xs:element maxOccurs="unbounded" name="Sequence">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" name="Content">
                <xs:complexType>
                    <xs:attribute name="ContentName" type="xs:string" use="required" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="Name" type="xs:string" use="required" />
    </xs:complexType>
</xs:element>

In the XSD, how can I tell to the ContentName attribute of the Content elements of Sequence to only accepts value declared in the ContentName of Content elements?

e.g: with the XML provided above, only contentName1 will be accepted in the Content of sequence.

like image 895
Gregoire Avatar asked Nov 15 '22 12:11

Gregoire


1 Answers

Identity constraint definitions are used for enforcing the unique, primary key and foreign key relations. you need to first define a key element for the content element and then use a keyref in the inner content element for the schema validator to enforce the condition you mentioned.
Refer the below link it has some examples as well, also the tutorial in xfront for xsd covers some examples -

http://www.w3.org/TR/xmlschema11-1/#Identity-constraint_Definition_details
http://www.xfront.com/files/xml-schema.html

like image 50
keshav84 Avatar answered Jun 19 '23 05:06

keshav84