[英]XML Schema Union getting “string” is not valid for the element error
I am an experienced programmer but just recently took on a job maintaining an app that uses xml schema. 我是一名经验丰富的程序员,但最近才开始维护一个使用xml架构的应用程序。 They want to add some validation on an item that accepts Longitude.
他们想要在接受经度的项目上添加一些验证。 They want to continue to accept a blank and also 0, 0.0000000, or if another value is entered they want to make sure that at the least it is in the United States.
他们希望继续接受空白以及0,0.0000000,或者如果输入了另一个值,他们希望确保至少它在美国。 (ie between -125 and -67)
(即介于-125和-67之间)
The current xml schema simply allows any value. 当前的xml架构只允许任何值。
<xs:element name="Location">
<xs:complexType>
<xs:sequence>
<xs:element name="LocLongitude"/>
</xs:sequence>
</xs:complexType>
</xs:element>
There are multiple venders sending this info in. Here is an example of what they may send: 有多个供应商发送此信息。以下是他们可能发送的内容的示例:
<Location>
<LocLongitude xsi:type="xsd:string"></LocLongitude>
</Location>
Now looking at what the users want I found that I can use a union to encapsulate multiple checks. 现在看看用户想要什么,我发现我可以使用union来封装多个检查。 This is what I am using now.
这就是我现在使用的。
<xs:element name="Location">
<xs:complexType>
<xs:sequence>
<xs:element name="LocLongitude" nillable="true">
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:double">
<xs:minInclusive value="0.00000000"/>
<xs:maxInclusive value="0.00000000"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:double">
<xs:minInclusive value="-125"/>
<xs:maxInclusive value="-67"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
It validates correctly if I use: 如果我使用它,它会正确验证:
<Location>
<LocLongitude />
</Location>
Now if I use what the current vendors are using (see below): 现在,如果我使用当前供应商正在使用的内容(见下文):
<Location>
<LocLongitude xsi:type="xsd:string"></LocLongitude>
</Location>
We get an error: 我们收到一个错误:
THE XSI:TYPE ATTRIBUTE VALUE '' IS NOT VALID FOR THE ELEMENT 'LOCLONGITUDE', EITHER BECAUSE IT IS NOT A TYPE VALIDLY DERIVED FROM THE TYPE IN THE SCHEMA, OR BECAUSE IT HAS XSI:TYPE DERIVATION BLOCKED.
XSI:类型属性值'对于元素'LOCLONGITUDE'是无效的,因为它不是来自模式中的类型的有效类型,或者因为它具有XSI:类型派生被阻止。
My question is, can I get this to work while still allowing the vendors to include xsi:type="xsd:string"? 我的问题是,我是否可以让这个工作,同时仍然允许供应商包括xsi:type =“xsd:string”?
No, the type chosen for xs:type
must be validly derived from the type provided by the associated element. 不,为
xs:type
type选择的xs:type
必须从关联元素提供的类型有效地派生。 You cannot on the one hand define a type that restricts the value space and on the other hand supports a broader xs:type
declaration. 您不能一方面定义限制值空间的类型,另一方面支持更广泛的
xs:type
声明。
See also: How to restrict the value of an XML element using xsi:type in XSD? 另请参阅: 如何在XSD中使用xsi:type限制XML元素的值?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.