2016-01-09 26 views
0

我有像ParcelNumber或像CoordinateXCoordinateY這樣的座標強制要求的這樣的要求,並且這兩個包含不同的XML層次結構。因此,如果輸入xml包含宗地編號,它應該返回成功,或者如果它包含兩個座標,那麼它應該返回成功。我創建了以下模式,如果我只發送宗地編號,它將返回成功,但是如果我發送Co-座標它會失敗,它要求發送包裹數量被wrong.How實現it.In果殼我有以下問題如何在XML模式中強制使用任一字段

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的想法

+0

如果這個問題是關於XML模式,標記與XSD的問題。否則,對XSD感興趣的人將無法找到它。接下來,如果您有關於XML Schema的問題,請始終發佈隨附的示例XML文檔。理想情況下,一個XML文檔應該對這個模式有效,另一個應該是無效的。 –

回答

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

@avinashHow我應該適合我的xsd,因爲層次結構是不同的,就像WorkLocationCoordinateType-> CoordinatesType-> WorkAreaType-> WorkLocationRoadType – peter

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> 
​ 
+0

甚至在填充座標後它顯示的是需要發送的包裹號碼 – peter

+0

然後添加minOccurs =「0」到宗地編號。 – jdweng

+0

如果我喜歡上述問題將得到解決,但如果我沒有把parcelnumber和座標爲null的東西,它仍然顯示爲空,如何修復它 – peter

相關問題