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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…