XMLSpy 忽略“minOccurs”?为什么这个 XML 有效?
请告诉我为什么 XML Spy 认为这是有效。仅供参考,这指定了一个 SQL 查询。这里是 XML:
<sideBar title="LabelSearch">
<searchLabel table="ID=*.companies">
<filter accessRight="r">
<and>
<filterElement argument="companies.Type" operator="=" value="Client"/>
</and>
</filter>
</searchLabel>
</sideBar>
问题:我不应该只允许在“and”标记内放入一个filterElement,而应该至少放入两个。如果我只有一个filterElement,我应该使用它而不使用周围的“and”标签。这里是XSD:
<xs:complexType name="filterGroupType">
<xs:sequence>
<xs:choice>
<xs:element name="or" type="filterGroupOrType"/>
<xs:element name="and" type="filterGroupAndType"/>
<xs:element name="filterElement" type="filterType"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="accessRight" type="accessRightSimpleType" use="required"/>
</xs:complexType>
<xs:complexType name="filterGroupAndType">
<xs:sequence minOccurs="2" maxOccurs="unbounded">
<xs:element name="or" type="filterGroupOrType" minOccurs="0"/>
<xs:element name="filterElement" type="filterType" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="filterGroupOrType">
<xs:sequence minOccurs="2" maxOccurs="unbounded">
<xs:element name="and" type="filterGroupAndType" minOccurs="0"/>
<xs:element name="filterElement" type="filterType" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="filterType">
<xs:attribute name="argument" type="xs:string" use="required"/>
<xs:attribute name="operator" type="operatorSimpleType" use="required"/>
<xs:attribute name="value" type="xs:anySimpleType"/>
<xs:attribute name="field" type="fieldTitleSimpleType"/>
</xs:complexType>
提前致谢。
Please enlighten me why XML Spy thinks this is valid. FYI, this specifies an SQL query. Here the XML:
<sideBar title="LabelSearch">
<searchLabel table="ID=*.companies">
<filter accessRight="r">
<and>
<filterElement argument="companies.Type" operator="=" value="Client"/>
</and>
</filter>
</searchLabel>
</sideBar>
The problem: I should not be allowed to put in only one filterElement inside the "and" tag, but at least two. If I only have one filterElement, I should use it without the surrounding "and" tag. Here the XSD:
<xs:complexType name="filterGroupType">
<xs:sequence>
<xs:choice>
<xs:element name="or" type="filterGroupOrType"/>
<xs:element name="and" type="filterGroupAndType"/>
<xs:element name="filterElement" type="filterType"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="accessRight" type="accessRightSimpleType" use="required"/>
</xs:complexType>
<xs:complexType name="filterGroupAndType">
<xs:sequence minOccurs="2" maxOccurs="unbounded">
<xs:element name="or" type="filterGroupOrType" minOccurs="0"/>
<xs:element name="filterElement" type="filterType" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="filterGroupOrType">
<xs:sequence minOccurs="2" maxOccurs="unbounded">
<xs:element name="and" type="filterGroupAndType" minOccurs="0"/>
<xs:element name="filterElement" type="filterType" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="filterType">
<xs:attribute name="argument" type="xs:string" use="required"/>
<xs:attribute name="operator" type="operatorSimpleType" use="required"/>
<xs:attribute name="value" type="xs:anySimpleType"/>
<xs:attribute name="field" type="fieldTitleSimpleType"/>
</xs:complexType>
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简而言之,
minOccurs="0"
。
而不是
。答案长一点。
就像 @Damien 所说,XML 是有效的,因为模式的这一部分允许“空”序列。
使用
您定义“此序列必须至少出现两次”。同时
允许序列中的这些元素不存在。打个比方,这就像在说“你必须点两次餐,但你不必吃你点的任何一顿饭”。相反,如果您希望始终拥有至少 2 个子元素,并且这些子元素可以是任意顺序的
或
元素,则应使用
和minOccurs="1"
minOccurs
的默认值为 1,因此您可以将其省略并保持代码简洁,更短。
选择其子级之一并重复选择至少minOccurs
次。如果至少其中一个选项可以具有minOccurs="0"
,则该选项也将允许“空”选择。Briefly
minOccurs="0"
.<xs:choice>
instead of<xs:sequence>
.A bit longer answer.
Just like @Damien said, that XML is valid because this part of your schema allows "empty" sequences.
With
<xs:sequence minOccurs="2" maxOccurs="unbounded">
You define that "this sequence must appear at least twice". At the same time<xs:element name="or" type="filterGroupOrType" minOccurs="0"/>
Allows these elements within the sequence to be absent. Metaphorically it is like saying "You must order a meal twice but you don't have to eat any of the meals you ordered."Instead if you want to always have at least 2 child elements and these children can be
<filterElement>
or<or>
elements in any order, you should use<xs:choice>
withminOccurs="1"
The default value for
minOccurs
is 1 so you can leave it out and keep your code cleaner and shorter.<xs:choice>
selects one of its children and repeats choosing at leastminOccurs
times. If at least one of choices can hasminOccurs="0"
then the choice will also allow "empty" selections.你在filterElement上有一个minOccurs =“0”。所以单个filterElement可以被看作是一个filterElement 0次,后面跟着一个filterElement。一个有效的序列。
也许您应该避免在架构中的 xs:sequence 和 xs:element 中混合 minOccurs ...
You have a minOccurs="0" on filterElement. So a single filterElement can be seen as a filterElement 0 times, followed by a filterElement. A valid sequence.
Maybe you should avoid mixing minOccurs in xs:sequence and xs:element in your schema...
您有一个必须出现两次的序列,但序列中的元素是可选的 (minOccurs="0")。序列本身不表示任何 XML,它只是定义它可以包含的可能元素以及它们必须采用的顺序。在这种情况下,完全有效的序列可以为空。
替换与;如果您不关心元素出现的顺序并且看起来您不关心,则将子元素上的 minOccurs 更改为 1 将起作用。如果您没有将子元素的 minOccurs 设置为 1,那么有效的选择仍然是出现 0 次的元素,并且您的 XML 将验证。如果您将子元素的 minOccurs 设置为 2,那么您将至少有 4 个元素,因为您将进行两次选择,并从每次出现两次的每个子元素中进行选择。
You have a sequence that must occur twice, but elements in the sequence are optional (minOccurs="0"). The sequence itself doesn't represent any XML, it just defines the possible elements it can contain and the order they must be in. A perfectly valid sequence can be empty in that case.
Replacing <xs:sequence> with <xs:choice> and changing minOccurs to 1 on the subelements will work if you do not care what order the elements appear in and it looks like you don't. If you don't set minOccurs to 1 on the children then a valid choice would still be the element occurring 0 times and your XML would validate. If you set minOccurs to 2 on the child elements then you would have a minimum of 4 elements since you would make a choice twice and pick from each child element occurring twice each time.