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
906 views
in Technique[技术] by (71.8m points)

xml - Understanding xsd:choice and minOccurs

I am having trouble understanding the behavior of the following XML schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="rootnode">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:choice minOccurs="1" maxOccurs="2">
          <xsd:element name="e1" minOccurs="1" maxOccurs="2"/>
          <xsd:element name="e2" minOccurs="0" maxOccurs="1"/>
        </xsd:choice>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

I expected at least one instance of either element <e1> or <e2> be required as a child of <rootnode>. Despite my expectations, an empty <rootnode> will validate against this schema:

 > xmllint --schema test.xsd empty.xml
 <?xml version="1.0" encoding="UTF-8"?>
 <rootnode>
 </rootnode>
 empty.xml validates

If I change the minOccurs attribute of element e2 to something other than "0", I get the behavior I originally expected.

  • It seems as though the mere absence of element <e2> counts as an occurrence of the xsd:choice in my example.

  • If this is the case, then how come this infinite number of occurrences does not violate the maxOccurs limit in my xsd:choice?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I tell you you can go to the shops at least once and at most twice, and each time you have a choice of what to buy: you can buy apples (either one apple or two apples), or you can buy oranges (either no oranges or one orange).

It's entirely possible that you will choose to go to the shops twice and on each occasion to buy no oranges. So you come back with nothing.


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

...