
I have an XML Schema I'm trying to lock down (to do more validation for me). I have an element with a required attribute that I restrict as shown below. <xsd:element name="MyElement1"> <xsd:complexType> <xsd:sequence> <xsd:element name="Description" minOccurs="0" type="xsd:string"/> <xsd:element maxOccurs="unbounded" ref="MyElement2"/> <xsd:element maxOccurs="unbounded" ref="MyElement3"/> <xsd:element maxOccurs="unbounded" ref="MyElement4"/> </xsd:sequence> <xsd:attribute name="scope" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="scope1"/> <xsd:enumeration value="scope2"/> <xsd:enumeration value="scope3"/> <xsd:enumeration value="scope4"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:element> What I want to do is have MyElement3 only allowable in a MyElement1 where scope == scope2 (for example). So have a rule on an element that restricts what element can contain it based on the attribute value. Is this possible? Any pointer to documentation doing something like this? Thanks!

Hello, This is a co-occurrance constraint and cannot be enforced in XML Schema. You need a Schematron constraint which restricts the presence of MyElement3 based on the value of the scope attribute: <xsd:element name="MyElement1"> <xsd:annotation> <xsd:appinfo> <pattern xmlns="http://purl.oclc.org/dsdl/schematron" name="Check MyElement3 with scope attr"> <rule context="MyElement1"> <assert test="(MyElement3 and @scope = 'scope2') or not(MyElement3)">MyElement3 is allowed only if @scope is 'scope2'.</assert> </rule> </pattern> </xsd:appinfo> </xsd:annotation> <xsd:complexType> <xsd:sequence> <xsd:element name="Description" minOccurs="0" type="xsd:string"/> <xsd:element maxOccurs="unbounded" ref="MyElement2"/> <xsd:element minOccurs="0" maxOccurs="unbounded" ref="MyElement3"/> <xsd:element maxOccurs="unbounded" ref="MyElement4"/> </xsd:sequence> <xsd:attribute name="scope" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="scope1"/> <xsd:enumeration value="scope2"/> <xsd:enumeration value="scope3"/> <xsd:enumeration value="scope4"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:element> In the XML instances you need to specify that the XML Schema contains Schematron constraints, that is insert at the beginning of the XML instance a processing instruction like: <?oxygen SCHSchema="path/to/schemaWithCOConstraint.xsd"?> This processig instruction is inserted automatically if you associate the schema with the XML instance using the Associate Schema action available on the Document toolbar and also on the Document -> XML Document menu. Regards, Sorin Timothy Reaves wrote:
I have an XML Schema I'm trying to lock down (to do more validation for me). I have an element with a required attribute that I restrict as shown below.
<xsd:element name="MyElement1"> <xsd:complexType> <xsd:sequence> <xsd:element name="Description" minOccurs="0" type="xsd:string"/> <xsd:element maxOccurs="unbounded" ref="MyElement2"/> <xsd:element maxOccurs="unbounded" ref="MyElement3"/> <xsd:element maxOccurs="unbounded" ref="MyElement4"/> </xsd:sequence> <xsd:attribute name="scope" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="scope1"/> <xsd:enumeration value="scope2"/> <xsd:enumeration value="scope3"/> <xsd:enumeration value="scope4"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> </xsd:complexType> </xsd:element>
What I want to do is have MyElement3 only allowable in a MyElement1 where scope == scope2 (for example). So have a rule on an element that restricts what element can contain it based on the attribute value.
Is this possible? Any pointer to documentation doing something like this? Thanks!
participants (2)
-
Sorin Ristache
-
Timothy Reaves