Using Anonymous Type Definitions

So far, all the element declarations we've used in the ch05_07.xsd schema have used the type attribute to indicate the new element's type. But what if you want to use a type only once? Do you have to go to the trouble of declaring it and naming it, all to use it in only one element declaration?

It turns out that there is an easier way. You can use an anonymous type definition to avoid having to define a whole new type that you'll reference only once. Using an anonymous type definition simply means that you enclose a <xsd:simpleType> or <xsd:complexType> element inside an <xsd:element> element declaration. In this case, you don't assign an explicit value to the type attribute in the <xsd:element> element because the anonymous type you're using doesn't have a name . (In fact, you can tell that an anonymous type definition is being used if an <xsd:complexType> element does not include a type attribute.)

Here's an example from ch05_07.xsd. In this case, I'll use an anonymous type definition for the <book> element. This element holds <bookTitle> , <pubDate> , <replacementValue> , and <maxDaysOut> elements. It will also have an attribute named bookID , so it looks like a good one to create from a complex type. Instead of declaring a separate complex type, however, I'll just put a <xsd:complexType> element inside the <xsd:element> element that declares <book> :

 <xsd:element name="book" minOccurs="0" maxOccurs="10">  <xsd:complexType>   .   .   .   </xsd:complexType>  </xsd:element> 

Now I'm free to add the elements I want inside the <book> elementwithout defining a named, separate complex type at all:

 <xsd:element name="book" minOccurs="0" maxOccurs="10">      <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:sequence>   </xsd:complexType>  </xsd:element> 

You can also use simple anonymous types. For example, the <maxDaysOut> element holds the maximum number of days that a book is supposed to be out. To set the maximum number of days that a book can be out to 14 , I use a new simple anonymous type so that I can use the maxExclusive facet like this:

 <xsd:element name="book" minOccurs="0" maxOccurs="10">      <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:complexType> </xsd:element> 

You can also include attribute declarations in anonymous type definitions, like this:

 xsd:element name="book" minOccurs="0" maxOccurs="10">     <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> 

Now I'll take a look at declaring empty elements.



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