Click here to Skip to main content
15,910,980 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Is it possible to use a xsd schema against an xml file that validates the xml file in the following ways:

1. if Attibute1 value is "XYZ" then Attibute2 value should be "ABC"?
if Attibute1 value is "WXYZ" then Attibute2 value should be "DEF"?

current xsd sample:

XML
<xs:attribute name="Attribute1" use="required" form="unqualified" >
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="XYZ" />
                    <xs:enumeration value="WXYZ" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>

        <xs:attribute name="Attribute2" use="required" form="unqualified" >
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="ABC" />
                    <xs:enumeration value="DEF" />
                    <xs:enumeration value="GHI" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>



I added the following schematron code:

XML
<xs:element name="ImageView" maxOccurs="unbounded" minOccurs="1" type="ImageView_Type" >
<xs:annotation>
    <xs:appinfo>
        <sch:pattern name="Co-occurrence constraint on attribute Title" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
        <!--<sch:rule context="ImageView">-->
        <sch:rule context="*[@Attribute1]">
        <sch:assert test="(@Attribute1 = 'XYZ' and @Attribute2 = 'ABC')">If the Attribute1 is "XYZ" then the Attribute2 must be "ABC".</sch:assert>
        </sch:rule>
        </sch:pattern>
    </xs:appinfo>
</xs:annotation>


but still it is not performing the validation , i am using MSXML4 for xml validation.
Posted
Updated 20-May-16 1:28am
v3

1 solution

XML
<xs:element name="ImageView" maxoccurs="unbounded" minoccurs="1" type="ImageView_Type" xmlns:xs="#unknown">
    <xs:annotation>
        <xs:appinfo>
            <sch:pattern name="Co-occurrence constraint on attribute Title" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
                <!--<sch:rule context="ImageView">-->
                <xs:attribute name="Attribute1" use="required" form="unqualified">
                    <xs:simpletype>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="XYZ" />
                            <xs:enumeration value="WXYZ" />
                        </xs:restriction>
                    </xs:simpletype>
                </xs:attribute>
            
                <xs:attribute name="Attribute2" use="required" form="unqualified">
                    <xs:simpletype>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="ABC" />
                            <xs:enumeration value="DEF" />
                            <xs:enumeration value="GHI" />
                        </xs:restriction>
                    </xs:simpletype>
                </xs:attribute>
                <sch:rule context="*[@Attribute1]">
                    <sch:assert test="(@Attribute1 = 'XYZ' and @Attribute2 = 'ABC')">If the Attribute1 is "XYZ" then the Attribute2 must be "ABC".</sch:assert>
                </sch:rule>
            </sch:pattern>
        </xs:appinfo>
    </xs:annotation>
</xs:element>
 
Share this answer
 
v2
Comments
George Jonsson 20-May-16 7:37am    
At least try to format the code. (I did it for you now)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900