我想验证这个XML:
<meta>
<house>
<big ... />
<little ... />
<big ... />
</house>
<flat>
<red ... />
<red ... />
<yellow ... />
</flat>
</meta>
我写了那个。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="meta">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="house">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='big' />
<xs:element name='little' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="flat">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='red'/>
<xs:element name='yellow'/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
但这并不能验证我的例子。 没有'房子'或'平',只有元,这是有效的。
哪里可能是我的问题?
答案 0 :(得分:0)
找到了!
解决方案:为每个“xs:sequence”添加“xs:choice”,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="meta">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name="house">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name='big' />
<xs:element name='little' />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="flat">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name='red'/>
<xs:element name='yellow'/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>