I want to use the below XSD to validate a particular XML. And at the same time the XSD should not validate few element from the below XML but the other elements should be validated.
For instance the sample XML is:
<args src="body">
<arg name="echo1">£*138</arg>
<arg name="echo2">a-a$138</arg>
<arg name="echo3">b-b$136</arg>
<arg name="echo4">£*136</arg>
</args>
And the XSD I am using is:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="args">
<xs:complexType>
<xs:sequence>
<xs:element name="arg" maxOccurs="9" minOccurs="1">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="argFilter">
<xs:attribute name="name" use="required" type="codeEnumeration" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="src" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="url"/>
<xs:enumeration value="body"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:simpleType name="codeEnumeration">
<xs:restriction base="xs:string">
<xs:enumeration value="echo1"/>
<xs:enumeration value="echo2"/>
<xs:enumeration value="echo3"/>
<xs:enumeration value="echo4"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="argFilter">
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="256"/>
<xs:pattern value="[a-zA-Z0-9 ,:?.\\\-_=%+@&!@#$%^*()\[\]+={}|\/:;,?`~'"<>]*"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
From the above XML, I do not want echo1
and echo4
to be validated, but all other element should be validated. So, basically it should allow any value in echo1
and echo4
but restrict the value of echo2
and echo3
to match the pattern value. How can I achieve this by modifying the above XSD?
You'll need to change your XML design or validate apart from XSD if you're limited to XSD 1.0. If you can use XSD 1.1, then Conditional Type Assignment will allow you to express relaxed validation constraints based upon an attribute value in XSD without changing your XML design.
See also: