我有一個XML架構,看起來像這樣:XSD:不允許空元素
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="A" minOccurs="0" maxOccurs="1"/>
<xs:element name="B" minOccurs="0" maxOccurs="1"/>
<xs:element name="C" minOccurs="0" maxOccurs="32"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
下是有效的,根據這個模式:
<root xsi:noNamespaceSchemaLocation="MySchema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</root>
不過,我想使以上XML無效。
更具體而言,我想要求: 1.該<root>
具有至少一個子元素,無論是一個<A>
,一個<B>
或<C>
,和 2.該<root>
具有至多一個<A>
孩子,最多一個<B>
孩子。
對此提出建議?
解決辦法是:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:sequence>
<xs:element name="A"/>
<xs:element name="B" minOccurs="0"/>
<xs:element name="C" minOccurs="0" maxOccurs="32"/>
</xs:sequence>
<xs:sequence>
<xs:element name="B"/>
<xs:element name="C" minOccurs="0" maxOccurs="32"/>
</xs:sequence>
<xs:sequence>
<xs:element name="C" minOccurs="1" maxOccurs="32"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
你的建議讓我回答了凱特。謝謝! – JaysonFix 2010-07-01 13:45:39