【问题标题】:'' is not a valid value of the atomic type 'xs:integer''' 不是原子类型 'xs:integer' 的有效值
【发布时间】:2021-10-12 21:09:24
【问题描述】:

xml 可以包含数据类型为整数的元素,也可以为空。我收到错误 '' is not a valid value of the atomic type 'xs:integer'

有没有办法只为这个元素指定最小长度,或者这个元素的值在 xsd 中是可选的?

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案1】:

    有两种方法:

    (a) 定义一个允许整数或零长度字符串的联合类型

    (b) 定义项目类型为xs:integer 的列表类型,minLength=0 和 maxLength=1。

    从验证的角度来看,两者是等效的。 (a) 更常见,但在我看来 (b) 更简单。如果您使用模式进行数据输入而不仅仅是验证,它们可能会给出不同的结果,例如,如果您使用的是模式感知 XSLT 或 XQuery,或 JAXP 样式的数据绑定。

    【讨论】:

      【解决方案2】:

      Michael Kay 给出了两种可能的解决方案,但第三种(在某些方面更简单)选项是将元素指示为nillable。当它可以为空时,您可以将其留空并使用xsi:nil="true" 将其指示为nil。

      但是,如果您无法控制 XML 以包含 xsi:nil,那么您需要采纳 Michael Kay 的建议之一。

      架构:

      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <xsd:element name="n">
              <xsd:complexType>
                  <xsd:sequence>
                      <xsd:element ref="count" minOccurs="0"/>
                  </xsd:sequence>
              </xsd:complexType>
          </xsd:element>
          <xsd:element name="count" type="xsd:integer" nillable="true" />
      </xsd:schema>
      

      XML:

      <n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <count xsi:nil="true" />
      </n>
      

      【讨论】:

      • 我从未见过使用xsi:nil 的充分理由。太丑了!
      猜你喜欢
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2021-12-26
      • 2013-02-02
      • 1970-01-01
      • 2013-02-27
      相关资源
      最近更新 更多