加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 为什么这个XDocument验证失败?

发布时间:2020-12-16 23:17:15 所属栏目:百科 来源:网络整理
导读:给定模式(匿名化,重命名关键点,其余部分省略): ?xml version="1.0" encoding="utf-8"?xs:schema id="inspec" targetNamespace="the_right_namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xs:element name="inspec" xs:complexType xs:all xs:elem
给定模式(匿名化,重命名关键点,其余部分省略):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="inspec"
    targetNamespace="the_right_namespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="inspec">
    <xs:complexType>
      <xs:all>
        <xs:element name="a_scalar_property" type="xs:int"/>
        <xs:element name="a_collection_property">
          <xs:complexType>
            <snip>
          </xs:complexType>
        </xs:element>
        <xs:element name="another_collection_property">
          <xs:complexType>                
            <snip>
          </xs:complexType>
        </xs:element>                       
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

和实例(使用VB xml文字声明):

Dim xDocument = 
<x:inspec xmlns:x='the_right_namespace'>
<a_collection_property/>
<another_collection_property/>
</x:inspec>

验证失败并显示消息命名空间’the_right_namespace’中的元素’inspec’具有不完整的内容.期望的可能元素列表:’a_scalar_property’.

为什么?根据W3Schools的说法,所有元素都是:

“all元素指定子元素可以按任何顺序出现,并且每个子元素可以出现零次或一次.”

省略a_scalar_property与将其包括零次相同.为什么此文档无法验证?

并且不要说“发布完整代码”之类的内容 – 这不是我的IP,而且我已将其匿名化了,原因很简单.它没有什么其他的东西,我已经用这个最小的例子进行了测试,它给出了相同的结果.

解决方法

您需要为xs:all中的每个可选元素指定minOccurs =“0”:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="inspec"
    targetNamespace="the_right_namespace"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
    <xs:element name="inspec">
        <xs:complexType>
            <xs:all>
                <xs:element name="a_scalar_property" type="xs:int" minOccurs="0" />
                <xs:element name="a_collection_property" minOccurs="0">
                    <xs:complexType>
                        <!-- snip -->
                    </xs:complexType>
                </xs:element>
                <xs:element name="another_collection_property" minOccurs="0">
                    <xs:complexType>
                        <!-- snip -->
                    </xs:complexType>
                </xs:element>
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读