Here I end my XML Video tutorial by covering complex types, sequence, groups, and a ton more! I also show you how to design schemas and XML files using the Eclipse design tools.
If you missed the past tutorials, here they are based on topic: XML Video Tutorial, XSL Video Tutorial, XPATH Video Tutorial, DTD Video Tutorial, Entity Video Tutorial and part 1 of my Schema Tutorial.
All of the code from the video and more follow the video.
If you like tutorials like this, tell Google
Sharing with others is always great
Code from the Video
BASEBALLPLAYERS.XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.newthinktank.com/baseballplayers/1.0"
xmlns:nttbp="http://www.newthinktank.com/baseballplayers/1.0" elementFormDefault="qualified">
<xs:annotation>
<xs:documentation>
This XML schema will be used for....
</xs:documentation>
</xs:annotation>
<xs:element name="baseballplayers" />
<xs:element name="player_name" type="xs:string" />
<xs:element name="current_age" type="xs:integer" />
<xs:element name="batting_average" type="xs:decimal" />
<xs:element name="starting" type="xs:boolean" />
<xs:element name="date_born" type="xs:date" />
<xs:element name="next_game_time" type="xs:time" />
<xs:element name="final_game" type="xs:dateTime" />
<xs:element name="time_in_game" type="xs:duration" />
<xs:element name="picture" type="xs:anyURI" />
<xs:element name="last_tweet">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="140" />
<xs:minLength value="2" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:simpleType name="position">
<xs:restriction base="xs:string">
<xs:enumeration value="Pitcher" />
<xs:enumeration value="Catcher" />
<xs:enumeration value="Infielder" />
<xs:enumeration value="outfielder" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="id_code">
<xs:restriction base="xs:string">
<xs:pattern value="\d{2}[a-zA-Z]{2,5}" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="prev_bat_avg">
<xs:list itemType="xs:decimal" />
</xs:simpleType>
<!-- You can also allow an element to be one or another type using union
<xs:simpleType name="message_to_fans">
<xs:union memberTypes="last_tweet fb_message"/>
</xs:simpleType> -->
<!-- Complex type elements can contain child elements and attributes
simple type elements can't as we saw -->
<!-- If you want to define an element that only contains one data type
value like the simpleType elements above, but need attributes
then use the simpleContent tag. Then you need to define what data
type you want to extend using extension -->
<xs:element name="year_born">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:int">
<xs:attribute name="home_state" type="xs:string"></xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<!-- To define an element that will contain only elements use the
complexType tag. <xs:sequence> defines the order the elements
are expected to be in -->
<!-- If you don't care if the elements are in order or even used,
replace <xs:sequence> with <xs:all> -->
<!-- If you don't care about the order, but want them to be included
use minOccurs set to one. maxOccurs can also be set to a specific
number, or unbounded if there is no limit. minOccurs & maxOccurs
are 1 by default
<xs:all>
<xs:element name="first_name" type="xs:string" minOccurs="1"/>
<xs:element name="last_name" type="xs:string" minOccurs="1"/>
</xs:all>
-->
<xs:element name="best_friend">
<xs:complexType>
<xs:sequence>
<xs:element name="first_name" type="xs:string"></xs:element>
<xs:element name="last_name" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- If you want to give an option to pick from one or another
element or elements, with sequence use xs:choice -->
<xs:element name="place_of_birth">
<xs:complexType>
<xs:choice>
<xs:element name="city_state" type="xs:string"></xs:element>
<xs:sequence>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
<!-- You can define empty elements that don't contain content
but do have attributes -->
<xs:complexType name="id_numbers">
<xs:attribute name="ss_number" type="xs:string"/>
<xs:attribute name="drivers_lic" type="xs:integer"/>
</xs:complexType>
<!-- If I wanted to create a custom type that could be used by
other elements I just create a named type. The last 2 elements
are called anonymous types. -->
<xs:complexType name="percent_stat">
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="year" type="xs:int"></xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- This is how I'd use those named types -->
<xs:complexType name="batting_stats">
<xs:sequence>
<xs:element name="batting_average"
type="nttbp:percent_stat">
</xs:element>
<xs:element name="obp" type="nttbp:percent_stat"></xs:element>
</xs:sequence>
</xs:complexType>
<!-- If you want to use elements and add a few more just use extension -->
<xs:complexType name="more_batting_stats">
<xs:complexContent>
<xs:extension base="nttbp:batting_stats">
<xs:sequence>
<xs:element name="slugging_percent" type="xs:float"></xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<!-- If you plan on using a group of elements through out a document, put
them in a group. -->
<xs:group name="at_bat_group">
<xs:sequence>
<xs:element name="at_bat_time" type="xs:dateTime"></xs:element>
<xs:element name="at_bat_balls" type="xs:int"></xs:element>
<xs:element name="at_bat_ks" type="xs:int"></xs:element>
<xs:element name="at_bat_result" type="xs:string"></xs:element>
</xs:sequence>
</xs:group>
<!-- You can then use the group of elements with ref
If you want to force the user to enter an attribute use required
If you want the attribute to be empty use use="prohibited"
You can also give the attribute a default with default="20" or
give it a fixed value with fixed="20" -->
<xs:element name="at_bat_pitcher">
<xs:complexType>
<xs:group ref="nttbp:at_bat_group"/>
<xs:attribute name="pitcher_name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<!-- You can also create a group of attributes for reuse -->
<xs:attributeGroup name="physique">
<xs:attribute name="player_height" type="xs:string"></xs:attribute>
<xs:attribute name="player_weight" type="xs:int"></xs:attribute>
</xs:attributeGroup>
<!-- You insert them with ref just like with groups of elements -->
<xs:element name="player_size">
<xs:complexType>
<xs:attributeGroup ref="nttbp:physique"></xs:attributeGroup>
</xs:complexType>
</xs:element>
</xs:schema>
BASEBALLPLAYERS.XML
<?xml version="1.0" encoding="UTF-8"?> <nttbp:baseballplayers xmlns:nttbp="http://www.newthinktank.com/baseballplayers/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.newthinktank.com/baseballplayers/1.0 baseballplayers.xsd "> <nttbp:player_name>Paul Smith</nttbp:player_name> <nttbp:current_age>23</nttbp:current_age> <nttbp:batting_average>.237</nttbp:batting_average> <nttbp:starting>true</nttbp:starting> <nttbp:date_born>1986-02-23</nttbp:date_born> <nttbp:next_game_time>14:30:00</nttbp:next_game_time> <nttbp:final_game>2012-09-20T14:30:00</nttbp:final_game> <nttbp:time_in_game>PT3H15M</nttbp:time_in_game> <nttbp:picture>http://www.pix.com/paulsmith.jpg</nttbp:picture> <nttbp:last_tweet>I hope I have a good game</nttbp:last_tweet> <nttbp:position>Infield</nttbp:position> <nttbp:id_code>34abc</nttbp:id_code> <nttbp:prev_bat_avg>.234 .212 .253</nttbp:prev_bat_avg> <nttbp:year_born home_state="PA">1986</nttbp:year_born> <nttbp:best_friend> <nttbp:first_name>Mark</nttbp:first_name> <nttbp:last_name>Roberts</nttbp:last_name> </nttbp:best_friend> <nttbp:place_of_birth> <nttbp:city_state>Adamsburg, PA</nttbp:city_state> </nttbp:place_of_birth> <nttbp:id_numbers ss_number="123-45-6789" drivers_lic="123456789" /> <nttbp:at_bat_pitcher pitcher_name="John Marks"> <nttbp:at_bat_time>2012-09-20T14:30:00</nttbp:at_bat_time> <nttbp:at_bat_balls>2</nttbp:at_bat_balls> <nttbp:at_bat_ks>3</nttbp:at_bat_ks> <nttbp:at_bat_result>Strikeout</nttbp:at_bat_result> </nttbp:at_bat_pitcher> </nttbp:baseballplayers>
is there anywhere to download the source file? Or how do you transfer the code to the eclipse editor so it recognizes it?
All of the code is under the video. If you drift the mouse over the code an icon with <> will pop up. Click on that icon to get the code without the line numbers