简体   繁体   English

获取“字符串”的XML Schema Union对元素错误无效

[英]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.

相关问题 WiX ComboBox元素在XML模式中无效 - WiX ComboBox Element is not valid in XML Schema 如果字符串元素包含有效的XML,是否需要CDATA才能针对模式进行验证/反序列化 - Is CDATA required to validate/deserialize against a schema if a string element contains valid XML 另一个String未被识别为有效的DateTime和XML元素 - Another String was not recognised as a valid DateTime And XML Element 将C#字符串验证为有效的XML模式anyURI - Verifying a C# string as a valid XML Schema anyURI 指定的架构无效:错误 - Schema specified is not valid:Error 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 获取错误字符串未识别为有效的DateTime - Getting error String was not recognized as a valid DateTime 从XMl元素导入时,字符串未被识别为有效的DateTime - String was not recognized as a valid DateTime,When importing from XMl element 使用XML模式验证字符串是否具有正确的XML格式(XML模式将允许具有任何属性的任何元素) - Validating a string has the correct XML format using XML schema (The XML schema would allow any element with any attribute) Linq To SQL指定的强制转换与Union无效 - Linq To SQL Specified cast is not valid error with Union
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM