Creating Choices

A choice lets you specify a number of elements, only one of which will be chosen . To create a choice in XML schemas, you use the <xsd:choice> element. The code that follows is an example. In this case, I'll change the transactionType type so that the borrower can borrow either several books or just one book. I do this by creating an <xsd:choice> element that holds both a <books> element and a <book> element. Note that, in this case, the <book> element needs to be made into a global element so I can refer to it in this choice. Thus, I remove it from the declaration of the <books> element, as you see here:

 xsd:complexType name="transactionType">     <xsd:sequence>        <xsd:element name="Lender" type="address"/>        <xsd:element name="Borrower" type="address"/>        <xsd:element ref="note" minOccurs="0"/>  <xsd:choice>   <xsd:element name="books" type="books"/>   <xsd:element ref="book"/>   <xsd:choice>  </xsd:sequence>     <xsd:attribute name="borrowDate" type="xsd:date"/> </xsd:complexType> <xsd:complexType name="books">     <xsd:sequence>         <xsd:element ref="book" minOccurs="0" maxOccurs="10" />     </xsd:sequence> </xsd:complexType> <xsd:element name="book">     <xsd:complexType>         <xsd:sequence>             <xsd:element name="bookTitle" type="xsd:string"/>             <xsd:element name="pubDate" type="xsd:date" minOccurs='0'/>             <xsd:element name="replacementValue" type="xsd:decimal"/>             <xsd:element name="maxDaysOut">                 <xsd:simpleType>                     <xsd:restriction base="xsd:integer">                         <xsd:maxExclusive value="14"/>                     </xsd:restriction>                 </xsd:simpleType>             </xsd:element>         <xsd:sequence>         <xsd:attribute name="bookID" type="catalogID"/>     </xsd:complexType> </xsd:element> 

Next, I'll take a look at creating sequences.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net