2015-10-17 27 views
-1

我有一個架構如下圖所示,我有一個關於架構如何使無論是在XML模式強制性領域

1.How使CourierNumberWorkLocationCoordinate mandatory.I幾個問題都使用一個全球型下面

EDIT所示 仍然沒有運氣如阿貝爾提到我修飾的模式,但它是failing.The輸入XML是

<?xml version="1.0" encoding="utf-8"?> 
<NOCPlantMapRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <NOCTypeID>0</NOCTypeID> 
    <WorkLocation> 
    <ParcelNumber>4545</ParcelNumber> 
    <Roads> 
     <WorkLocationRoad> 
     <RoadName>dubai road</RoadName> 
     </WorkLocationRoad> 
    </Roads> 
    <WorkArea> 
     <WorkArea> 
     <Coordinates> 
      <WorkLocationCoordinate> 
      <CoordinateX>56</CoordinateX> 
      <CoordinateY>23</CoordinateY> 
      </WorkLocationCoordinate> 
     </Coordinates> 
     <Communities /> 
     </WorkArea> 
    </WorkArea> 
    </WorkLocation> 
</NOCPlantMapRequest> 

和SCH EMA是

<?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="NOCReference" minOccurs="0" type="xsd:string" /> 
     <xsd:element name="NOCTypeID" minOccurs="0" type="xsd:unsignedByte" /> 
     <xsd:element name="NOCTypeName" minOccurs="0" type="xsd:string" /> 
     <xsd:element name="ApplicationName" minOccurs="0" type="xsd:string" /> 
     <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:choice> 
     <xsd:sequence> 
     <xsd:element name="ParcelNumber" type="ParcelNumberType" /> 
     </xsd:sequence> 
     <xsd:sequence> 
     <xsd:element name="WorkArea" type="WorkAreaType" /> 
     </xsd:sequence> 
    </xsd:choice> 
    </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: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> 

但如下圖所示 不適我收到錯誤。

Error - Line 6, 12: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 12; cvc-complex-type.2.4.d: Invalid content was found starting with element 'Roads'. No child element is expected at this point. 
Error - Line 19, 24: org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 24; cvc-complex-type.2.4.d: Invalid content was found starting with element 'Communities'. No child element is expected at this point. 

我的主要要求是要麼ParcelNumberWorkLocationCoordinateType應該呈現,出了什麼問題?

回答

0

您需要修改「CourierNumber」元素的定義以包含必需的屬性。更新此代碼:

<xsd:element name="CourierNumber" type="xs:string"/> 

這樣:如果你定義元素的類型

<xsd:element name="CourierNumber"> 
    <xsd:complexType> 
    <xsd:simpleContent> 
    <xsd:extension base="xsd:string"> 
    <xsd:attribute name="Option" type="xsd:string" use="required"/> 
    </xsd:extension> 
    </xsd:simpleContent> 
    </xsd:complexType> 
    </xsd:element> 

借鑑Element-Mandatory Attribute declaration in XSD Schema

 <xsd:element name="Coordinates"> 
     <xsd:complexType> 
      <xsd:sequence> 
      <xsd:element name="WorkLocationCoordinate" 
      type="Test" minOccurs="1" maxOccurs="99" 
      /> 
      </xsd:sequence> 
     </xsd:complexType> 
     </xsd:element> 
     <xsd:complexType name="Test"> 
    <xsd:sequence> 
     <xsd:element name="CoordinateX" type="xsd:unsignedByte" /> 
     <xsd:element name="CoordinateY" type="xsd:unsignedByte" /> 
     </xsd:sequence> 
      </xs:complexType> 

內聯,你不能設定名稱,以便去除type="WorkLocationCoordinate"屬性(然後還有name="WorkLocationCoordinate"屬性)。

如果你想使用這些屬性,那麼你需要分離元素和類型定義,例如<xsl:element name="WorkLocationCoordinate" type="WorkLocationCoordinate"/><xsl:complexType name="WorkLocationCoordinate">...</xsl:complexType>

有關您在評論中提到錯誤的詳細信息,請參閱the type attribute cannot be present with either simpleType or complexType

+0

你可以修改它根據我的問題,因爲當我把類型WorkLocationCoordinate 我得到的錯誤類型屬性不能與簡單類型或複雜類型一起存在 – peter

+0

try Manraj

+0

相同的結果「屬性不能以簡單類型或複雜類型出現」 – peter

4

1.How使CourierNumber或WorkLocationCoordinate mandatory.I已經使用下面

在所示一個全球型的給定代碼,這兩個定義並不緊密(一個在第二個子級,另一個深度嵌套),所以我在理解你的意思時有些困難。

不可能在相同級別(相同XPath)上具有相同名稱但類型不同的兩個元素。如果你嘗試,你會得到(取決於你的XSD解析器):

E [Xerces] cos-element-consistent: Error for type 'LocationType'. 
Multiple elements with name 'WorkArea', with different types, appear in the model group. 

如果你可以使用XSD 1.1,你can work around this by using assertions。由於WorkArea中的唯一信息是座標,因此我假定您的意思是在第一個位置中CourierNumber或在第二個位置中WorkArea之間切換,但不是兩者(實際上,它會幫助您顯示具有所需變體的實例文檔) 。

如果是如何應用在元素名稱CourierNumber和WorkLocationCoordinate的類型,因爲「CourierNumber」已包含類型爲xsd:unsignedShort的

這是你真正的問題所在。由於您不使用命名類型(所有東西都是具有匿名複雜類型定義的大元素),因此您無法引用這些類型。一種解決方案是重複定義,但這可能會變得乏味並且有其侷限性。

我在下面提供的解決方案只是將您的代碼重構爲「類型優先」方法。也就是說,不是一個大的(難以閱讀的)元素定義,而是一小塊命名的類型定義。意見可能會有所不同,但我相信這更具可讀性,而且它更加靈活。 A good introduction of this approach and what its drawbacks and features are can be found on XFront

<?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"> 

    <!-- belonging to SO question https://stackoverflow.com/questions/33183835/how-to-make-either-of-the-fields-mandatory-in-xml-schema --> 

    <xsd:complexType name="CoordinatesType"> 
     <xsd:sequence> 
      <xsd:element name="WorkLocationCoordinate" type="WorkLocationCoordinateType"/> 
     </xsd:sequence> 
    </xsd:complexType> 

    <xsd:simpleType name="CourierNumberType"> 
     <xsd:restriction base="xsd:unsignedShort"/> 
    </xsd:simpleType> 

    <xsd:complexType name="WorkLocationCoordinateType"> 
     <xsd:sequence> 
      <xsd:element name="CoordinateX" type="xsd:unsignedByte" /> 
      <xsd:element name="CoordinateY" type="xsd:unsignedByte" /> 
     </xsd:sequence> 
    </xsd:complexType> 

    <xsd:complexType name="WorkAreaType"> 
     <xsd:sequence> 
      <xsd:element name="WorkArea"> 
       <xsd:complexType> 
        <xsd:sequence> 
         <xsd:element name="Coordinates" type="CoordinatesType" /> 
        </xsd:sequence> 
       </xsd:complexType> 
      </xsd:element> 
     </xsd:sequence> 
    </xsd:complexType> 

    <xsd:complexType name="LocationType"> 
     <xsd:choice> 
      <xsd:sequence> 
       <xsd:element name="CourierNumber" type="CourierNumberType" /> 
       <xsd:element name="Roads" /> 
      </xsd:sequence> 
      <xsd:sequence> 
       <xsd:element name="Roads" /> 
       <xsd:element name="WorkArea" type="WorkAreaType" /> 
      </xsd:sequence> 
     </xsd:choice> 
    </xsd:complexType> 

    <xsd:element name="Request"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="Location" type="LocationType" /> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

這證實了無論是這樣的:

<Request> 
    <Location> 
     <CourierNumber>12</CourierNumber> 
     <Roads></Roads> 
    </Location> 
</Request> 

或者這樣:

<Request> 
    <Location> 
     <Roads></Roads> 
     <WorkArea> 
      <WorkArea>     
       <Coordinates> 
        <WorkLocationCoordinate> 
         <CoordinateX>34</CoordinateX> 
         <CoordinateY>66</CoordinateY> 
        </WorkLocationCoordinate> 
       </Coordinates> 
      </WorkArea> 
     </WorkArea> 
    </Location> 
</Request> 
+0

我不希望CourierNumber被座標取代,而應該出現在它自己的位置,即在這裏的頂部place.So要麼CourierNumber在頂部的地方或座標在底部應該提出 – peter

+1

@ peter,抱歉,這是不明確的我你的問題,但方法是一樣的。讓我再看一次。 – Abel

+0

我不確定同樣的答案適用於那些情況,是嗎? – peter