基于某些约束验证xml

时间:2018-01-24 13:30:59

标签: xml xsd-validation

我是编写xml验证的新手,有人可以在下面的查询中提供帮助。 如果XML的每个元素具有相同的值,即'N',我想要通过XSD验证失败。如果它有其他组合,它应该通过验证。

请参阅以下XML和XSD:

<noticeOfEligibility>
    <displayHCCTable>N</displayHCCTable>
    <displaySNAPTable>N</displaySNAPTable>
    <displayTANFTable>N</displayTANFTable>
    <displayCCAPTable>N</displayCCAPTable>
    <displayLIHEAPTable>N</displayLIHEAPTable>
    <displayLIHEAPShareTable>N</displayLIHEAPShareTable>        
</noticeOfEligibility>

XSD:

 <xs:complexType name="noticeOfEligibility">
    <xs:sequence>
        <xs:element name="displayHCCTable" type="xs:string" />               
        <xs:element name="displaySNAPTable" type="xs:string" />               
        <xs:element name="displayTANFTable" type="xs:string" />                
        <xs:element name="displayCCAPTable" type="xs:string" />                
        <xs:element name="displayLIHEAPTable" type="xs:string" />               
        <xs:element name="displayLIHEAPShareTable" type="xs:string" />                
        <xs:element name="displayEmgyLIHEAPTable" type="xs:string" />                
        <xs:element name="displayLIHEAPVendorTable" type="xs:string" />               
    </xs:sequence>
</xs:complexType>

1 个答案:

答案 0 :(得分:0)

您可以尝试使用xs:assert验证器(我添加了一些元素,但您明白了)

<xs:complexType name="noticeOfEligibility"> <xs:sequence> <xs:element name="displayHCCTable" type="xs:string" /> <xs:element name="displaySNAPTable" type="xs:string" /> <xs:element name="displayTANFTable" type="xs:string" /> <!-- more elements here --> </xs:sequence> <xs:assert test="displayHCCTable != 'N' and displaySNAPTable != 'N' and displayTANFTable != 'N'"/> </xs:complexType>

适用于XSD 1.1。