内部的xsd选择有多种选择

时间:2015-05-27 19:33:38

标签: xml validation xsd sax multiple-choice

我对同一个xsd有两种类型的请求:

请求1

<REQUEST>
    <ID_CLIENTE>123456</IDCLIENTE>
    <NUMERO_TARJ>11111111111111</NUMERO_TARJ>
    <MES_VENC>04</MES_VENC>
    <ANO_VENC>18</ANO_VENC>
    <MONTO>150.00</MONTO>
</REQUEST>

请求2

<REQUEST>
    <ID_CLIENTE>123456</IDCLIENTE>
    <DATOS>1112124548797987984489848468448</DATOS>
    <MONTO>150.00</MONTO>
</REQUEST>

需要以下标签:

  • ID_CLIENTE
  • 蒙托

以下标签是有条件的:

  • DATOS
  • NUMERO_TARJ
  • MES_VENC
  • ANO_VENC

如果我发送DATOS标签,我无法发送NUMERO_TARJ,MES_VENC和ANO_VENC。

如果我发送NUMERO_TARJ,我必须始终发送MES_VENC和ANO_VENC,但我无法发送DATOS标签。

我有这个XSD,但它不正确,它不验证我想要的(总是我必须使用所有标签):

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="REQUEST">
         <xs:complexType>
             <xs:all>
                 <xs:element type="xs:long" name="ID_CLIENTE" minOccurs="1" maxOccurs="1" />
                 <xs:element type="xs:string" name="MONTO" minOccurs="1"  maxOccurs="1"/>
                 <xs:element minOccurs="1" maxOccurs="1" ref="opcionales" />
             </xs:all>
         </xs:complexType>
    </xs:element>
    <xs:element name="opcionales" abstract="true" />
    <xs:element type="xs:string" name="DATOS" substitutionGroup="opcionales"/>
    <xs:element name="infotarjeta" substitutionGroup="opcionales">
        <xs:complexType>
            <xs:all>
                <xs:element type="xs:long" name="TARJ_NUM" minOccurs="1"  maxOccurs="1"/>
                <xs:element type="xs:int" name="MES_VENC" minOccurs="1"  maxOccurs="1"/>
                <xs:element type="xs:int" name="ANO_VENC" minOccurs="1"  maxOccurs="1"/>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

请帮我修复此XSD。

谢谢!

2 个答案:

答案 0 :(得分:1)

  

(DATOS)或(TARJ_NUM和MES_VENC和ANO_VENC)是必需条件

这表示您希望在choiceelement之间sequence

<xs:element name="REQUEST">
     <xs:complexType>
         <xs:sequence>
             <xs:element type="xs:long" name="ID_CLIENTE" minOccurs="1" maxOccurs="1" />
             <xs:choice>
                 <xs:element type="xs:string" name="DATOS"/>
                 <xs:sequence>
                    <xs:element type="xs:long" name="TARJ_NUM" minOccurs="1"  maxOccurs="1"/>
                    <xs:element type="xs:int" name="MES_VENC" minOccurs="1"  maxOccurs="1"/>
                    <xs:element type="xs:int" name="ANO_VENC" minOccurs="1"  maxOccurs="1"/>
                 </xs:sequence>
             </xs:choice>
             <xs:element type="xs:string" name="MONTO" minOccurs="1"  maxOccurs="1"/>
         </xs:sequence>
     </xs:complexType>
</xs:element>

这将验证您问题中的示例消息。

答案 1 :(得分:0)

XSD 1.1 中,您可以使用断言来执行您想要的操作:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">

    <xs:element name="REQUEST">
        <xs:complexType>
            <xs:all>
                <xs:element type="xs:long" name="ID_CLIENTE"/>
                <xs:element type="xs:string" name="MONTO"/>
                <xs:element type="xs:string" name="DATOS" minOccurs="0"/>
                <xs:element type="xs:long" name="TARJ_NUM" minOccurs="0"/>
                <xs:element type="xs:int" name="MES_VENC" minOccurs="0"/>
                <xs:element type="xs:int" name="ANO_VENC" minOccurs="0"/>
            </xs:all>
            <xs:assert test="(boolean(DATOS) and not(TARJ_NUM) and not(MES_VENC) and not(ANO_VENC))
                or
                (boolean(TARJ_NUM) and boolean(MES_VENC) and boolean(ANO_VENC) and not(DATOS))"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

我不知道如何使用 XSD 1.0 来做到这一点。