XML Video Tutorial 7

XML Schema TutorialHere 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


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:nttbp="http://www.newthinktank.com/baseballplayers/1.0" elementFormDefault="qualified">

		This XML schema will be used for....

<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:restriction base="xs:string">
			<xs:maxLength value="140" />	
			<xs:minLength value="2" />	

<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:simpleType name="id_code">
	<xs:restriction base="xs:string">
		<xs:pattern value="\d{2}[a-zA-Z]{2,5}" />

<xs:simpleType name="prev_bat_avg">
	<xs:list itemType="xs:decimal" />

<!-- 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:extension base="xs:int">
    			<xs:attribute name="home_state" type="xs:string"></xs:attribute>
<!-- 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:element name="first_name" type="xs:string" minOccurs="1"/>	
			<xs:element name="last_name" type="xs:string" minOccurs="1"/>	
	<xs:element name="best_friend">
    		<xs:element name="first_name" type="xs:string"></xs:element>
    		<xs:element name="last_name" type="xs:string"></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:element name="city_state" type="xs:string"></xs:element>
					<xs:element name="city" type="xs:string"/>	
					<xs:element name="state" type="xs:string"/>	
    <!-- 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"/>
    <!-- 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:extension base="xs:decimal">
    			<xs:attribute name="year" type="xs:int"></xs:attribute>
    <!-- This is how I'd use those named types -->

    <xs:complexType name="batting_stats">
    		<xs:element name="batting_average"
    		<xs:element name="obp" type="nttbp:percent_stat"></xs:element>
    <!-- If you want to use elements and add a few more just use extension -->

    <xs:complexType name="more_batting_stats">
    		<xs:extension base="nttbp:batting_stats">
    				<xs:element name="slugging_percent" type="xs:float"></xs:element>

<!-- 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: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>

<!-- 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:group ref="nttbp:at_bat_group"/>
  		<xs:attribute name="pitcher_name" type="xs:string" use="required"/>

<!-- 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>
<!-- You insert them with ref just like with groups of elements -->
	<xs:element name="player_size">
    	<xs:attributeGroup ref="nttbp:physique"></xs:attributeGroup>


<?xml version="1.0" encoding="UTF-8"?>
<nttbp:baseballplayers xmlns:nttbp="http://www.newthinktank.com/baseballplayers/1.0" 
xsi:schemaLocation="http://www.newthinktank.com/baseballplayers/1.0 baseballplayers.xsd ">
<nttbp:player_name>Paul Smith</nttbp:player_name>
	<nttbp:last_tweet>I hope I have a good game</nttbp:last_tweet>
	<nttbp:prev_bat_avg>.234 .212 .253</nttbp:prev_bat_avg>
	<nttbp:year_born home_state="PA">1986</nttbp:year_born>
		<nttbp:city_state>Adamsburg, PA</nttbp:city_state>
	<nttbp:id_numbers ss_number="123-45-6789" drivers_lic="123456789" />
	<nttbp:at_bat_pitcher pitcher_name="John Marks">


7 Responses to “XML Video Tutorial 7”

  1. Stephen Sweet says:

    is there anywhere to download the source file? Or how do you transfer the code to the eclipse editor so it recognizes it?

    • admin says:

      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

  2. Lakshma says:

    Bro u are awesome.. i don’t know how much experience you have, its wonderful… How long you think it will take for beginner to all this.

    Thank you …

    • Derek Banas says:

      Thank you 🙂 You are very kind. XML isn’t that complex. It is basically a way to create custom tags. If you experiment a bit with your own data you’ll get it. Feel free to ask questions.

  3. Steven says:

    Hi.. the tutorial is awesome two thumbs up! ^_^ i hope you can help me out for this.. Iam working on the codes for UAPI travelport and i want to submit this link to them as ping request http://demo.travelportuniversalapi.com/(S(ohhnqhl4ehkcc5lg2hmycdog))/Home/SampleCode you can choose the language you want. i have my credentials in my mail and i dont have any idea on how to send a request ping to them.. hope you can help me out..
    this is the main steps link
    this is the sending ping request link

    HELP me out please
    best regards

  4. Rudy says:

    Dear Mr Banas,

    I only wanted to report that as the xsd is written not all the tags into the xml are validated into Eclipse.
    An example is PositionType (I suffixed by Type) and IdCodeType(also suffixed).
    If I enter blablabla into Position it is not reported.
    If I enter aa12345 into IdCode it is also not reported.
    Do you have any clue why ?
    If I use the xsd to validate the xml online then it is the same.Some tags are validated some tags are not.
    But some types tags are validated.
    Example : If I put ‘aa’ in current_age then Eclipse underlines in red the value.

    Do you have any idea why ?

Leave a Reply

Your email address will not be published.