My program can return 2 XMLs like:
<RESPONSE>
<ERROR_ID>1</ERROR_ID>
<ERROR_MESSAGE>Parse error</ERROR_MESSAGE>
</RESPONSE>
<RESPONSE>
<ERROR_ID>2</ERROR_ID>
<ERROR_MESSAGE>Unexpected attribute</ERROR_MESSAGE>
</RESPONSE>
I try write some XSD file to validate if they are ok. Here what I end up with:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="RESPONSE" type="Response"/>
<xsd:complexType name="Response">
<xsd:all>
<xsd:element name="ERROR_ID" type="ErrorId"/>
<xsd:element name="ERROR_MESSAGE" type="ErrorMessage"/>
</xsd:all>
</xsd:complexType>
<xsd:simpleType name="ErrorId">
<xsd:restriction base="xsd:positiveInteger">
<xsd:enumeration value="1"/>
<xsd:enumeration value="2"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ErrorMessage">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Parse error"/>
<xsd:enumeration value="Unexpected attribute"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
It validates fine but I'm thinking if can I link up error id with error message to not let pass validation file where error id=2 will go with message from error with id=1:
<RESPONSE>
<ERROR_ID>2</ERROR_ID>
<ERROR_MESSAGE>Parse error</ERROR_MESSAGE>
</RESPONSE>
There is a good way to do this? My program returns of course a lot more error ids and message.
Maybe a better question is if I should even expect such a validation from XSD?
XSD 1.0 cannot constrain the value of one element based upon the value of another element.
XSD 1.1 could do so using xsd:assert
, however you should reconsider your design...
Alternative design suggestions:
Don't check the ERROR_ID
-ERROR_MESSAGE
pairing in XSD.
Concatenate the error id with the error message:
<Error>1. Parse error</Error>
Used fixed attributes against more specific error element names:
<ParseError id="1" message="Parse error"/>
<UnexpectedAttributeError id="2" message="Unexpected attribute"/>
None of these alternative designs require XSD 1.1.