如何在XML模式中使任一字段成为必需字段

时间:2016-01-09 08:00:24

标签: c# asp.net .net xml xsd

我有ParcelNumberCoordinateXCoordinateY等坐标的要求,这两个要素在XML中包含不同的层次结构。因此,如果输入xml包含地块编号,它应该返回成功,或者如果它包含两个坐标,那么它应该返回成功。我创建了以下模式,如果我只发送包裹编号,它将返回成功,但是如果我发送Co-纵坐标它会失败,它要求发送错误的包裹号码。如何实现它。在果壳中我有以下问题

1>If i send both coordinates  and no parcel number it should return success
2>IF i send x coordinate then it should fail and ask to send Y co ordinate

以下是我到目前为止所做的事情

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            attributeFormDefault="unqualified" elementFormDefault="qualified">
    <xsd:element name="NOCPlantMapRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Applicationtype" minOccurs="0" type="xsd:string"/>
                <xsd:element name="RelatedNOCRefNumber" minOccurs="0" type="xsd:string"/>
                <xsd:element name="WorkLocation" minOccurs="1" maxOccurs="1" type="LocationType"></xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:complexType name="LocationType">
        <xsd:all>
            <xsd:element name="ParcelNumber" type="ParcelNumberType" maxOccurs="1"/>
            <xsd:element name="WorkArea" type="WorkAreaType" maxOccurs="1"/>
            <xsd:element name="Roads" type="RoadListType" maxOccurs="1"/>
        </xsd:all>
    </xsd:complexType>
    <xsd:complexType name="RoadListType">
        <xsd:sequence>
            <xsd:element name="WorkLocationRoad" type="WorkLocationRoadType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="WorkLocationRoadType">
        <xsd:sequence>
            <xsd:element name="RoadName" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CommunitiesListType">
        <xsd:sequence>
            <xsd:element name="WorkLocationRoad" type="WorkLocationRoadType" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:simpleType name="ParcelNumberType">
        <xsd:restriction base="xsd:string"/>
    </xsd:simpleType>
    <xsd:complexType name="WorkAreaType">
        <xsd:sequence>
            <xsd:element name="WorkArea" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Coordinates" minOccurs="1" type="CoordinatesType"/>
                        <xsd:element name="Communities" type="CommunitiesListType" maxOccurs="1"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CoordinatesType">
        <xsd:sequence>
            <xsd:element name="WorkLocationCoordinate" type="WorkLocationCoordinateType"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="WorkLocationCoordinateType">
        <xsd:sequence>
            <xsd:element name="CoordinateX" type="xsd:string"/>
            <xsd:element name="CoordinateY" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

修改 知道如何在这里正确使用choice

2 个答案:

答案 0 :(得分:2)

您可以这样做: -

<xs:choice>
  <xs:sequence>
    <xs:element name="ParcelNumber" />
    <xs:element name="CoOrdinates" minOccurs="0" />
  </xs:sequence>
  <xs:sequence>
    <xs:element name="CoOrdinates" />
    <xs:element name="ParcelNumber" minOccurs="0" />
  </xs:sequence>
</xs:choice>

答案 1 :(得分:1)

试试这个

  <xsd:complexType name="WorkLocationCoordinateType">
        <xsd:sequence>
            <xsd:element name="CoordinateX" type="xsd:string" minOccurs="1"/>
            <xsd:element name="CoordinateY" type="xsd:string" minOccurs="1"/>
        </xsd:sequence>
    </xsd:complexType>
​