XML 根据 XMl 字段中的条件使用 xsd 进行验证

XML validation using xsd based on conditions in the XMl fields

我有一个 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>

是否可以使用 XSD 进行条件验证?。 例子。

*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

如果 header = "OK" 和值 = "0"
header = "NOK" 和值=“0” 然后XML无效

是否可以使用 XML 模式进行这种验证?

您可以在 complexType 中使用 xs:assert 节点,这样您就可以在 xsd 文件中执行 xpath 2.0 查询。 这仅适用于 XSD 1.1

第一次测试的例子:

   <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>

虽然我不完全确定下面的兄弟姐妹