tags:

views:

21

answers:

1

I'm currently trying to create an XSD where I have a which can have only on of the following values:

<media_type>wmv</media-type>

or

<media_type>h264</media_type>

or

<media_type>mov</media_type>

I have found the <xs:choice/> element, but if I construct a complex type as such:

 <xs:element name="media_type" type="xs:string">
  <xs:complexType>
   <xs:sequence>
    <xs:element ref="h264"/>
    <xs:element ref="wmv"/>
    <xs:element ref="flash"/>
   </xs:sequence>
   <xs:attribute name="media_id" use="required" type="xs:integer"/>
  </xs:complexType>
 </xs:element>

It will look for elements under <media_type/>. Is there a way to check the contents of an element in XSD?

+3  A: 

Yep!

<xs:simpleType name="mediaType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="wmv"/>
    <xs:enumeration value="h264"/>
    <xs:enumeration value="mov"/>
  </xs:restriction>
</xs:simpleType>
S..
Awesome, thanks! Worked fine when I wrapped it in an <xs:element/> and moved the 'name' to <xs:element/>.
Drew
@Drew: the name on the `simpleType` is the name of the type. Also "wrapping it in an element" is really giving the element the type of the contained `simpleType` --- which you could also do by putting the name of the type (defined elsewhere in the document) in the `type` attribute: `<xs:element name="media_type" type="mediaType"/>`
Porges
Ah, so rather than directly declaring the simpleType where I want to use it, I can create references? I'm just starting to use XSD, so I've only been making my XSD look like my XML, rather than creating reusable parts.
Drew