开发者

I have this xsd and I want to make an optional element is this possible?

I have the 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>

The last element can be:

<category>
</category>

Or it can be:

<subject>
</subject>

But how do I represent his as optional (category|subject) in the XSD?


You can use the xs:choice element to denote alternatives:

<xs:choice>
    <xs:element name="category" type="xs:string"/>
    <xs:element name="subject" type="xs:string"/>
</xs:choice>


Try this:

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜