另一種選擇是讓你使用繼承。你得到的XML是不是很漂亮,但你得到完全相同的內容你想要的:
<xsd:element name="field" type="field" abstract="true" />
<xsd:element name="subfield" type="xsd:string" />
<xsd:complexType name="field" abstract="true" />
<xsd:complexType name="subfield">
<xsd:complexContent>
<xsd:extension base="field">
<xsd:sequence>
<xsd:element ref="subfield" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="no-subfield">
<xsd:complexContent mixed="true">
<xsd:extension base="field">
<xsd:attribute name="name" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
那麼你得到的XML將包含以下(假設你有xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
地方申報)
<field xsi:type="subfield">
<subfield>your stuff here</subfield>
</field>
或
<field xsi:type="no-subfield">your other stuff</field>
最重要的是,它不允許
<field xsi:type="subfield">
Text you don't want
<subfield>your stuff here</subfield>
More text you don't want
</field>
你不能使用xs:choice嗎? – 2014-09-17 14:19:39