如何在 <xs:all> 中将项目设为可选? [英] How do I make items optional in <xs:all>?

查看:11
本文介绍了如何在 <xs:all> 中将项目设为可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的xsd.这些所有字段可以存在或不存在,并且顺序不可预测.

I have such xsd. These all fields can exist or not and in unpredictable order.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:element name="request">
<xs:complexType>
  <xs:all  minOccurs="0">
    <xs:element ref="field1"/>
    <xs:element ref="field2"/>
    <xs:element ref="field3"/>
    <xs:element ref="field4"/>
    <xs:element ref="field5"/>
  </xs:all>
</xs:complexType>
</xs:element>

</xs:schema>

xml 中不存在field4,validator 说他正在等待field4,但他不应该这样说.那么有什么问题呢?

field4 doesn't exist in xml and validator says that he is waiting for field4, but he shouldn't say this. So what is wrong?

w3cschools.com

<xs:element name="person">
<xs:complexType>
<xs:all minOccurs="0">
  <xs:element name="firstname" type="xs:string"/>
  <xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>

上面的例子表明firstname"和lastname"元素可以以任意顺序出现,每个元素可以出现零次或一次!

The example above indicates that the "firstname" and the "lastname" elements can appear in any order and each element CAN appear zero or one time!

推荐答案

您需要将 minOccurs 放在单个元素上,而不是 <xs:all>,即

You need to put the minOccurs on the individual elements, not the <xs:all>, i.e.

<xs:all>
    <xs:element ref="field1" minOccurs="0"/>
    <xs:element ref="field2" minOccurs="0"/>
    <xs:element ref="field3" minOccurs="0"/>
    <xs:element ref="field4" minOccurs="0"/>
    <xs:element ref="field5" minOccurs="0"/>
</xs:all>

minOccurs="0" 放在 <xs:all> 上表示可以省略整个组,而不是单个元素.

Putting minOccurs="0" on the <xs:all> is saying that entire group may be omitted, not individual elements.

请参阅 XML 架构文档.

这篇关于如何在 &lt;xs:all&gt; 中将项目设为可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆