xml - Enforcing validation of a secondary attribute based on another attribute in XSD -
question
is there way enforce subtype
"percent"
if basetype
"percent"
?
i mean, have validation fail if basetype
"percent"
subtype
isn't "percent"
?
xml partial
<quantity name="abc" basetype="percent" value="2" subtype="percent"/>
xsd pertinent sections
<xs:element name="thing"> <xs:complextype> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="basetype" type="basetype" use="required"/> <xs:attribute name="value" type="xs:double" use="required"/> <xs:attribute name="subtype" type="subtype" use="required"/> </xs:complextype> </xs:element> <xs:simpletype name="basetype"> <xs:restriction base="xs:string"> <xs:enumeration value="fixed"/> <xs:enumeration value="percent"/> </xs:restriction> </xs:simpletype> <xs:simpletype name="subtype"> <xs:restriction base="xs:string"> <xs:enumeration value="foo"/> <xs:enumeration value="percent"/> <xs:enumeration value="bar"/> </xs:restriction> </xs:simpletype>
xsd 1.0
xsd 1.0 cannot represent constraint.
xsd 1.1
xsd 1.1 can represent constraint using xs:assert
:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:vc="http://www.w3.org/2007/xmlschema-versioning" vc:minversion="1.1"> <xs:element name="quantity"> <xs:complextype> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="basetype" type="basetype" use="required"/> <xs:attribute name="value" type="xs:double" use="required"/> <xs:attribute name="subtype" type="subtype" use="required"/> <xs:assert test="(@basetype != 'percent') or (@subtype = 'percent')"/> </xs:complextype> </xs:element> <xs:simpletype name="basetype"> <xs:restriction base="xs:string"> <xs:enumeration value="fixed"/> <xs:enumeration value="percent"/> </xs:restriction> </xs:simpletype> <xs:simpletype name="subtype"> <xs:restriction base="xs:string"> <xs:enumeration value="foo"/> <xs:enumeration value="percent"/> <xs:enumeration value="bar"/> </xs:restriction> </xs:simpletype> </xs:schema>
Comments
Post a Comment