[英]XSD Restriction on local complexType elements
我有一个具有本地complexType元素的架构,如下所示。
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Line1" type="xs:string" minOccurs="0"/>
<xs:element name="Line2" type="xs:string" minOccurs="0" maxOccurs="100"/>
<xs:element name="Location" minOccurs="0" maxOccurs="100">
<xs:complexType>
<xs:sequence>
<xs:element name="XCoordinate" type="xs:decimal" minOccurs="0"/>
<xs:element name="YCoordinate" type="xs:decimal" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
我正在尝试如下扩展这个complexType
<xs:complexType name="InternalAddressType">
<xs:complexContent>
<xs:restriction base="AddressType">
<xs:sequence>
<xs:element name="Location" >
</xs:element>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
我收到以下错误
**Error for type 'InternalAddressType'. The particle of the type is not a valid restriction of the particle of the base.
谁能帮我了解我在做什么。 看来问题在于位置是本地的ComplexType。 但是我无法更改它,因为我是从客户端获取xsd的,只需要扩展Loaction。 我该如何解决这种情况。 欢迎其他任何建议。
您说对了:您不能限制Location
因为它是由本地complexType
定义的。
甚至包括完全相同组件的AddressType
restriction
也将类似地失败:
<xs:complexType name="InternalAddressType">
<xs:complexContent>
<xs:restriction base="AddressType">
<xs:sequence>
<xs:element name="Line1" type="xs:string" minOccurs="0"/>
<xs:element name="Line2" type="xs:string" minOccurs="0" maxOccurs="100"/>
<xs:element name="Location" minOccurs="0" maxOccurs="100">
<xs:complexType>
<xs:sequence>
<xs:element name="XCoordinate" type="xs:decimal" minOccurs="0"/>
<xs:element name="YCoordinate" type="xs:decimal" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
如果不尝试覆盖Location
,那么上面的定义就可以了。
鉴于您无法更改AddressType
来提取本地complexType
,该怎么办? 根据您的要求,也许您可以使用xs:extension
扩展AddressType
并根据需要定义自己的MyLocation
元素,而忽略原始的Location
元素(或从不创建它-是可选的)。 或者,也许完全解耦的InternalAddressType定义对您有用。 如果这些可能性都不满足您的目的,请在注释中说明您最终目标的要求,也许我们会找到合适的答案。
如果您能够使用XSD 1.1(遗憾的是,没有多少人),则可以以断言的形式定义您的限制-因为它精确地指出了您想要应用的其他条件,因此它是一种更加实用的机制,我认为,与其提供必须与原始内容保持紧密对应关系的替代内容模型。
XSD 1.1当前在Saxon和Xerces中实现。
另一种解决方案是两阶段验证。 使用客户端的架构来确保文档符合客户端定义的约束,然后使用您自己的架构(或其他技术)来验证文档是否符合您定义的其他约束。 无论如何,这可能是一种架构上更清洁的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.