Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

xsd - How do I make items optional in <xs:all>?

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>

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 says

<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>

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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

See XML Schema docs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...