基于XMl字段中的条件使用xsd进行XML验证

时间:2015-06-03 06:55:39

标签: xml xsd xsd-validation

我有一个XML

std::ostream& operator<<(std::ostream& out, const Music &in)
{
  return out << in.m_name.length() << in.m_name.c_str() 
        << in.m_artist.length() << in.m_artist.c_str();
}

std::istream& operator>>(std::istream& is, Music &in)
{
  // pseudo code
  // read length, read characters of name put into "in"
  // read length, read characters of artist put into "in"
}

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

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

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

使用XML Schema可以进行这种验证吗?

1 个答案:

答案 0 :(得分:0)

您可以使用xs:assert中的complexType节点,以便在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>

我不完全确定以下兄弟姐妹