2011-06-07 40 views
0

我有XSD:我有這個xsd,我想做一個可選的元素,這可能嗎?

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="UploadXSD" 
    elementFormDefault="qualified" 
    xmlns="http://tempuri.org/UploadXSD.xsd" 
    xmlns:mstns="http://tempuri.org/UploadXSD.xsd" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="video"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="title" minOccurs="1" type="xs:string"></xs:element> 
     <xs:element name="description" type="xs:string"></xs:element> 
     <xs:element name="contributor" type="xs:string"></xs:element> 
     <xs:element name="cateogry" type="xs:string"></xs:element> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

</xs:schema> 

最後一個元素可以是:

<category> 
</category> 

或者可以這樣:

<subject> 
</subject> 

但是我怎麼代表他作爲可選(類別|主題)在XSD?

回答

1

試試這個:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="UploadXSD" 
elementFormDefault="qualified" 
xmlns="http://tempuri.org/UploadXSD.xsd" 
xmlns:mstns="http://tempuri.org/UploadXSD.xsd" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<xs:element name="video"> 
<xs:complexType> 
    <xs:sequence> 
    <xs:element name="title" minOccurs="1" type="xs:string"></xs:element> 
    <xs:element name="description" type="xs:string"></xs:element> 
    <xs:element name="contributor" type="xs:string"></xs:element> 
    <xs:choice> 
     <xs:element name="category" type="xs:string"/> 
     <xs:element name="subject" type="xs:string"/> 
    </xs:choice>  
    </xs:sequence> 
</xs:complexType> 
</xs:element> 

</xs:schema> 
3

可以使用xs:choice元素來表示的替代品:

<xs:choice> 
    <xs:element name="category" type="xs:string"/> 
    <xs:element name="subject" type="xs:string"/> 
</xs:choice> 
相關問題