子元素之间没有重复元素? [英] No repeat elements among child elements?
问题描述
假设我有以下复杂类型:
Imagine I have the following complex types:
<xs:complexType name="pet">
<xs:sequence>
<xs:element name="add" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="pets">
<xs:sequence>
<xs:element name="dogs" type="pet" minOccurs="0" maxOccurs="1" />
<xs:element name="cats" type="pet" minOccurs="0" maxOccurs="1" />
<xs:element name="hamsters" type="pet" minOccurs="0" maxOccurs="1" />
<xs:element name="horses" type="pet" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
这意味着我可以像这样指定我的 xml:
This means that I can specify my xml like this:
<pets>
<dogs>
<add>Fido</add>
<add>Pluto</add>
</dogs>
<cats>
<add>Zorro</add>
<add>Batman</add>
</cats>
<hamsters>
<add>Bob</add>
</hamsters>
<horses>
<add>Mr Horse</add>
</horses>
</pets>
但我真正想做的是让我的架构要求至少定义一种 pet
类型.如果我将 pets
类型更改为:
But what I really would like to do is to have my schema demand that at least one of the pet
types are defined. If I change my pets
type to:
<xs:complexType name="pets">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="dogs" type="pet" />
<xs:element name="cats" type="pet" />
<xs:element name="hamsters" type="pet" />
<xs:element name="horses" type="pet" />
</xs:choice>
</xs:complexType>
这种方式使它起作用,因为现在要求我至少定义一个 pet
.但是,现在可以拥有多个相同类型的 pet
元素.这是我想要避免的.以下内容现在有效:
This sort of makes it work because it is now demanded that I have at least one pet
defined. However, it is now valid to have multiple pet
elements of the same type. This is what I want to avoid. The following is now valid:
<pets>
<dogs>
<add>Pluto</add>
</dogs>
<dogs>
<add>Fido</add>
</dogs>
</pets>
那么,是否有可能每种类型只接受一个 pet
元素?
So, is it possible to only accept one pet
element per type?
推荐答案
XSD 1.0
@5gon12eder建议:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="pets">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="dogs" type="pet"/>
<xs:element name="cats" type="pet" minOccurs="0"/>
<xs:element name="hamsters" type="pet" minOccurs="0"/>
<xs:element name="horses" type="pet" minOccurs="0"/>
</xs:sequence>
<xs:sequence>
<xs:element name="cats" type="pet"/>
<xs:element name="dogs" type="pet" minOccurs="0"/>
<xs:element name="hamsters" type="pet" minOccurs="0"/>
<xs:element name="horses" type="pet" minOccurs="0"/>
</xs:sequence>
<xs:sequence>
<xs:element name="hamsters" type="pet"/>
<xs:element name="dogs" type="pet" minOccurs="0"/>
<xs:element name="cats" type="pet" minOccurs="0"/>
<xs:element name="horses" type="pet" minOccurs="0"/>
</xs:sequence>
<xs:sequence>
<xs:element name="horses" type="pet"/>
<xs:element name="dogs" type="pet" minOccurs="0"/>
<xs:element name="cats" type="pet" minOccurs="0"/>
<xs:element name="hamsters" type="pet" minOccurs="0"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="pet">
<xs:sequence>
<xs:element name="add" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
XSD 1.1
使用xs:assert
保证pets
至少有一个孩子:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="pets">
<xs:complexType>
<xs:sequence>
<xs:element name="dogs" type="pet" minOccurs="0"/>
<xs:element name="cats" type="pet" minOccurs="0"/>
<xs:element name="hamsters" type="pet" minOccurs="0"/>
<xs:element name="horses" type="pet" minOccurs="0"/>
</xs:sequence>
<xs:assert test="*"/>
</xs:complexType>
</xs:element>
<xs:complexType name="pet">
<xs:sequence>
<xs:element name="add" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
这篇关于子元素之间没有重复元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!