0

I have an XML

<order>
<details type = "Edit" header = "OK" value ="1">
<details type = "Edit" header = "NOK" value ="1">
<details type = "Edit" header = "Reject" value ="1">
<details type = "Edit" header = "Accept" value ="1">
</order>

Is it possible to do the conditional validation using XSD ?. Example.

*If
 header = "OK"  and value= "1"  
 header = "NOK" and value= "0"
 then  XML Valid
*If
 header = "OK"  and value= "1"  
 header = "NOK" and value= "1"
 then  XML Valid
*If
 header = "OK"  and value= "0"  
 header = "NOK" and value= "1"
 then  XML Valid

If header = "OK" and value= "0"
header = "NOK" and value= "0" then XML Invalid

Is this kind of validation is possible using XML Schema ?

1 Answers1

0

You can use the xs:assert node in your complexType so that you can do an xpath 2.0 query inside your xsd file. This only works with XSD 1.1

example for your first test :

   <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="details" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="details">
    <xs:complexType>
      <xs:attribute name="type" type="xs:string" use="required"/>
      <xs:attribute name="header" type="xs:string" use="required"/>
      <xs:attribute name="value" type="xs:integer" use="required"/>
      <xs:assert test="((@header = 'OK') and (@value = 1)) and (./following-sibling::details/@header = 'NOK' and ./following-sibling::details/@value eq 0 )/>
      <xs:assert test="...."/>
      <xs:assert test="..."/>
    </xs:complexType>
  </xs:element>

I'm not fully sure about the following-sibling though

flafoux
  • 2,080
  • 1
  • 12
  • 13