标记值的xsd验证不为null

时间:2014-07-03 07:07:48

标签: xml tags xsd xsd-validation

我的Xml是:

    <LineService>
        <LineSrId>SR_CP_JD_144084-1</LineSrId>
        <MSNumber/>
        <AccountNo>1320116817829261</AccountNo>
    </LineService>

我的xsd是:

<element name="LineService" maxOccurs="unbounded" minOccurs="1">
    <complexType>
        <sequence>
            <element type="string" name="LineSrId" maxOccurs="1" minOccurs="0" nillable="true"/>
            <element name="MSISDN" maxOccurs="1" minOccurs="0" nillable="true">
                <simpleType>
                    <restriction base="string">
                        <pattern value="\d{10,18}|"></pattern>
                    </restriction>
                </simpleType>
            </element>
            <element type="string" name="AccountNo" maxOccurs="1" minOccurs="0" nillable="true"/>
        </sequence>
    </complexType>
</element>

我想检查MS号码或帐号号码可以为空,但对于请求,两者都不应为空。其中至少有一个不应为空且具有值。

1 个答案:

答案 0 :(得分:0)

您正在寻找xsd定义来制作<AccountNo><MSNumber>或两者,但不是有效的。通常的方法是组合<xs:choice><xs:sequence>和minOccurs属性。请注意,在您的xsd中,您的元素名称是MSISDN,在您的XML中是MSNumber,我在XML中使用MSNumber,这是我的解决方案:

<schema xmlns="http://www.w3.org/2001/XMLSchema">
...
<element name="AccountNo" type="string"/>
<element name="MSNumber">
    <simpleType>
        <restriction base="string">
            <pattern value="\d{10,18}|"></pattern>
        </restriction>
    </simpleType>
</element>
<element name="LineService" maxOccurs="unbounded" minOccurs="1">
    <complexType>
        <sequence>
            <element type="string" name="LineSrId" maxOccurs="1" minOccurs="0" nillable="true"/>
            <xs:choice>
                <xs:sequence>
                    <xs:element ref="MSNumber"/>
                    <xs:element ref="AccountNo" minOccurs="0"/>
                </xs:sequence>
                <xs:element ref="AccountNo"/>
            </xs:choice>            
        </sequence>
    </complexType>
</element>
...
</schema>

希望得到这个帮助,