我有一个如下所示的架构,我有一些关于架构的问题
1.如何制作CourierNumber或WorkLocationCoordinate强制。我使用了如下所示的一种全局类型
编辑 仍然没有运气,因为 Abel 提到我修改了架构,但它失败了。输入 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>
Run Code Online (Sandbox Code Playgroud)
模式是
<?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>
Run Code Online (Sandbox Code Playgroud)
但我收到如下所示的错误无效。
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.
Run Code Online (Sandbox Code Playgroud)
我的主要要求是ParcelNumber或者WorkLocationCoordinateType应该出现,出了什么问题?
1.如何强制使用 CourierNumber 或 WorkLocationCoordinate。我使用了如下所示的一种全局类型
在您给定的代码中,这两个定义并不紧密(一个在第二个子级别,另一个是深度嵌套的),所以我很难理解您的意思。
不可能在同一级别(相同 XPath)有两个名称相同但类型不同的元素。如果你尝试一下,你会得到(取决于你的 XSD 解析器):
E [Xerces] cos-element-consistent: Error for type 'LocationType'.
Multiple elements with name 'WorkArea', with different types, appear in the model group.
Run Code Online (Sandbox Code Playgroud)
如果您可以使用 XSD 1.1,则可以通过使用 assertions 来解决此问题。由于其中的唯一信息WorkArea是坐标,我假设您的意思是CourierNumber在第一个位置或WorkArea第二个位置之间切换,但不能同时在这两个位置之间切换(实际上,这将有助于显示包含您想要的变体的实例文档)。
如果是这样,如何应用元素名称 CourierNumber 和 WorkLocationCoordinate 中的类型,因为 'CourierNumber' 已经包含类型 xsd:unsignedShort
这就是你真正的问题所在。由于您不使用命名类型(一切都是具有匿名复杂类型定义的一个大元素),因此您无法引用这些类型。一种解决方案是重复定义,但这会变得乏味并且有其局限性。
我在下面为您提供的解决方案只是将您的代码重构为“类型优先”的方法。也就是说,您得到的不是一个大的(难以阅读的)元素定义,而是一小块命名的类型定义。意见可能会有所不同,但我相信这更具可读性,而且绝对更灵活。一个很好的介绍这一做法,其弊端和功能是什么都可以在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 /sf/ask/2322868481/ -->
<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>
Run Code Online (Sandbox Code Playgroud)
这验证了:
<Request>
<Location>
<CourierNumber>12</CourierNumber>
<Roads></Roads>
</Location>
</Request>
Run Code Online (Sandbox Code Playgroud)
或这个:
<Request>
<Location>
<Roads></Roads>
<WorkArea>
<WorkArea>
<Coordinates>
<WorkLocationCoordinate>
<CoordinateX>34</CoordinateX>
<CoordinateY>66</CoordinateY>
</WorkLocationCoordinate>
</Coordinates>
</WorkArea>
</WorkArea>
</Location>
</Request>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1119 次 |
| 最近记录: |