Wednesday, July 24, 2013

Java WebService Tutorial - Part 05 ( XSD )


In this tutorial  we will look in detail about XSD (XmlSchemaDefinition) in details now....

XSD : 

It is used in WSDL Document to define the data types of Method takes .Such as Input Parameters data types,Output Parameter data types,attribute types and return types.

It defines element that can appear in Document.

It defines attributes that can appear in the Document.

It defines the order of Child element and define no.of child elements.

It defines whether an elemnt is empty or can include text.
 
It defines default and fixed value for elements and attributes.

It can define in the WSDL itself or it can be refer externally by importing the XSD URL in the WSDL just like JavaScript and CSS.

It's an alternative to DTD's.More Powerful then DTD.(Document Type Definition)

Multiple Schema's can refer in one WSDL.Allows re-usability.Allow Namespace.

We can define our own dataype derived from the standard types.

Root Element of XSD :
  
<schema> : This is the root element of any XML Schema Document.It can contain attribute also.

 Syntax:
<? xml version="1.0" ? >
<xs:schema>
------
------
</xs:schema>

Ex:

<?xml version="1.0"?>

<xs:schema xmlns:"http://www.w3.org/2001/XMLSchema" targetNameSpace="http://www.ayaz.com" xmlns="http://www.ayaz.com" elementFromDefault="qualified">
...............
...............

</xs:schema>

Here xs:schema  xmlns follows the namespace define the w3 standards. targetNamspace schema defines the elements defines by this schema comes from ayaz.com.elementfromDefault specifies that every element should be namespace qualified.


Simple Example : (note.xml)
------------------------------------
<?xml version="1.0"?>
<note>
<from>Tom</from>
<to>Jack</to>
<heading>Remainders</heading>
<body> Dont'forget</body>
</note>

note.xsd :  Defines element for note.xml

<?xml version="1.0"?>
<xs:schema xmlns:"http://www.w3.org/2001/XMLSchema" targetNameSpace="http://www.ayaz.com" xmlns="http://www.ayaz.com" elementFromDefault="qualified">
.
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:String"/>
<xs:element name="from" type="xs:String/>
<xs:element name="heading" type="xs:String/>
<xs:element name="body" type="xs:String/>
 </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

We will look on each of the element in detail in later part.So i have the xsd for my xml element now i can refer in my Document .

<?xml version="1.0"?>
< note xmlns="http://www.ayaz.com" xmlns=xsi="http://www.w3org.2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ayaz.com/note.xsd">
<from>Tom</from>
<to>Jack</to>
<heading>Remainders</heading>
<body> Dont'forget</body>
</note>
Red color highlighted - tells the Schema validator that all elements used in this XML Document are declared in "http://www.ayaz.com"

Blue color highlighted - tells once you have the XML Schema instances we can use the SchemaLocation Attribute.

Brown Color highlighted - tells the namespace used for the xsd and it's location.

2. XSD Elements:

 XSD elements can be classified as "Single" and Complex" Element. 

a.Simple Element :

A Simple element does not have any attribute and other elements.

It can contain only Text.The Text can be of any type (String,boolean,Int)or it can be custom type define by the user.

We can also add restrictions such as limit the content or to match the specific pattern.

Synatx: 

<xs:element name="------ " type="------"/>

Ex: <xs:element name="firstname" type="xs:String"/>

Default & Fixed Value :

Default - This value is taken when there is no value is provided.
Fixed - This value is taken assigned automatically to the element and cannot assign other values.

Ex:
<xs:element name="firstname" type="xs:String" default="John "/>

<xs:element name="color" type="xs:String" fixed="red"/>


b. XSD Attributes:

Simple elements cannot have attributes. If an element is specified with attribute then it is considered ad complex type element.But a attribute always declared as   simple type.

Synatx :

<xs:attribute name="-----" type="----"/>

Ex:

<xs:attribute name="lang" type="xs:String" />

<lastname lang="en">Smith</lastname> -- Refering in XML Document.

Note: Default and fixed behave same way as the does for simple element.

Optional & required

Attribute can be specify as optional or required one.Attributes are optional by default to make the attribute declared as required we should declared by the "use" keyword.

<xs:attribute name="lang" type="xs:String"  use="required'/>

c. XSD Restrictions /Facets   :

<xs:restriction> is used to set restrcitions.

Restriction on Simple Element

Ex: To accept values between 0-120

<xs:element name="age">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="120"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>


To set Restrictions on set of values use the enumeration

<xs:element name="car">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Audi"/>
      <xs:enumeration value="Golf"/>
      <xs:enumeration value="BMW"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

Length Restriction

 <xs:element name="password">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:length value="8"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
 

Constraint          Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

3. Complex XSD Elements :

A complex element is an XML element that contains other elements and/or attributes.
There are four kinds of complex elements:
  • empty elements
  • elements that contain only other elements
  • elements that contain only text
  • elements that contain both other elements and text.
empty element :

An empty complex element cannot have contents, only attributes.

An empty XML element

<product prodid="1345" /> 

 Ex:
<xs:element name="product">
  <xs:complexType>
    <xs:attribute name="prodid" type="xs:positiveInteger"/>
  </xs:complexType>
</xs:element> 

Element which have elements only :

An "elements-only" complex type contains an element that contains only other elements.

An XML element, "person", that contains only other elements:

<person>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</person>

You can define the "person" element in a schema, like this:
<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Text- Only :

A complex text-only element can contain text and attributes.

This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content. When using simple content, you must define an extension OR a restriction within the simpleContent element, like this:
<xs:element name="somename">
  <xs:complexType>
    <xs:simpleContent>
     
        ....
        ....
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

OR

<xs:element name="somename">
  <xs:complexType>
    <xs:simpleContent>
     
        ....
        ....
      </xs:restriction>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

Mixed Complex Elements:
A mixed complex type element can contain attributes, elements, and text.

An XML element, "letter", that contains both text and other elements:
<letter>
  Dear Mr.<name>John Smith</name>.
  Your order <orderid>1032</orderid>
  will be shipped on 2001-07-13.
</letter>
The following schema  declares the "letter" element:

  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="orderid" type="xs:positiveInteger"/>
      <xs:element name="shipdate" type="xs:date"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

4 . Indicators
There are seven indicators:
Order indicators:
  • All
  • Choice
  • Sequence
Occurrence indicators:
  • maxOccurs
  • minOccurs
Group indicators:
  • Group name
  • attributeGroup name

Mostly Used indicators are discussed below :

Sequence Indicator

The <sequence> indicator specifies that the child elements must appear in a specific order:
<xs:element name="person">
   <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Occurence Indicator 

Occurrence indicators are used to define how often an element can occur.

maxOccurs Indicator
The <maxOccurs> indicator specifies the maximum number of times an element can occur:
<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string" maxOccurs="10"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.

minOccurs Indicator 

 The <minOccurs> indicator specifies the minimum number of times an element can occur:
<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="full_name" type="xs:string"/>
      <xs:element name="child_name" type="xs:string"
      maxOccurs="10" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.

To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement


<<Prev                            Next >>                   Index

---------------------------------------------------------------------------------------------                                        

Please provide your comments...



Technology Blogs
blogs

26 comments:

  1. A nice way create xsd from xml for beginners to understand. I like the way you format text with color & explain each point in detail. It's an excellent explanation I found ever. But there should not be any typos in such an amazing article. Have a look at this series & correct typos. Thanks for creating such useful tutorial.

    ReplyDelete
  2. I take pleasure in, lead to I discovered exactly what
    I was having a look for. You've ended my four day lengthy hunt!
    God Bless you man. Have a great day. Bye

    Also visit my webpage Christian Louboutin Pumps

    ReplyDelete
  3. I do believe all of the concepts you've offered to your post.

    They're very convincing and can definitely work. Nonetheless, the posts are very brief for novices.
    Could you please extend them a bit from subsequent time?
    Thank you for the post.

    Also visit my weblog - Beats By Dre USA

    ReplyDelete
  4. What's up, yes this post is actually pleasant and I have learned lot of things from it
    regarding blogging. thanks.

    Feel free to visit my weblog: Beats By Dre Solo

    ReplyDelete
  5. Your mode of describing the whole thing in this post is actually pleasant, every one be capable of simply understand it, Thanks a lot.


    Feel free to visit my web-site louis vuitton outlet store

    ReplyDelete
  6. I like the helpful information you provide in your articles.
    I will bookmark your weblog and check again here frequently.

    I'm quite certain I'll learn plenty of new stuff right
    here! Good luck for the next!

    Here is my web site :: New Balance Sale

    ReplyDelete
  7. Hola! I've been reading your web site for a while now and
    finally got the bravery to go ahead and give you a
    shout out from Atascocita Tx! Just wanted to mention keep up the great
    job!

    Check out my web site New Balance Outlet

    ReplyDelete
  8. Nice post. I was checking constantly this blog and I'm impressed!

    Very useful info particularly the last part :) I care for such
    info a lot. I was looking for this particular info for a long time.
    Thank you and best of luck.

    my blog post; Dr Dre Beats Studio

    ReplyDelete
  9. Greate pieces. Keep posting such kind of information on your blog.
    Im really impressed by it.
    Hello there, You have performed an excellent job.
    I'll definitely digg it and individually suggest to my friends.

    I am confident they'll be benefited from this
    website.

    my website - Dr Dre Beats

    ReplyDelete
  10. Awesome! Its truly remarkable piece of writing, I have got much clear
    idea regarding from this post.

    Here is my web blog :: Beats Monster

    ReplyDelete
  11. Unquestionably consider that which you stated.
    Your favorite reason appeared to be on the net
    the simplest thing to keep in mind of. I say to you,
    I certainly get irked whilst people think about concerns that they
    just don't know about. You controlled to hit the nail upon the highest as smartly as
    defined out the entire thing with no need side effect , other folks could take a signal.
    Will likely be back to get more. Thanks

    My web-site; Dre Headphones

    ReplyDelete
  12. Hi there mates, its fantastic post concerning tutoringand
    fully explained, keep it up all the time.

    my homepage: New Balance Sale

    ReplyDelete
  13. Awesome! Its actually awesome piece of writing, I have got much clear idea regarding from this
    article.

    Also visit my website Christian Louboutin Heels

    ReplyDelete
  14. constantly i used to read smaller content which as well clear
    their motive, and that is also happening with this paragraph which I am reading at this place.


    Review my webpage :: Replica Louis Vuitton

    ReplyDelete
  15. I'm truly enjoying the design and layout of your blog.
    It's a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often. Did you hire out a developer to create
    your theme? Outstanding work!

    My website :: Louis Vuitton Replica

    ReplyDelete
  16. Generally I do not read article on blogs, but I wish to say that this write-up very
    compelled me to take a look at and do it! Your writing taste has been surprised me.
    Thank you, quite great article.

    My page ... Louis Vuitton Outlet Store (http://louisvuittonaustralia-sale.onnistu.com/)

    ReplyDelete
  17. This article will help the internet viewers for building up new
    website or even a weblog from start to end.

    Feel free to surf to my page; Christian Louboutin Sydney

    ReplyDelete
  18. Good day very cool site!! Guy .. Beautiful ..
    Amazing .. I will bookmark your web site and take the feeds also?

    I am happy to seek out a lot of helpful information right here within the post,
    we need develop extra techniques on this regard, thank you
    for sharing. . . . . .

    Here is my webpage Christian Louboutin

    ReplyDelete
  19. Hello! I could have sworn I've visited this site before but
    after looking at a few of the posts I realized it's new to me.
    Anyhow, I'm certainly pleased I discovered it and
    I'll be book-marking it and checking back regularly!


    Feel free to surf to my page ... Cheap Louis Vuitton Bags

    ReplyDelete
  20. Its like you read my mind! You seem to know a lot about this,
    like you wrote the book in it or something. I think that you can do with some pics to drive the message home
    a bit, but instead of that, this is great blog. A fantastic read.
    I will definitely be back.

    My page: Christian Louboutin Online

    ReplyDelete
  21. Does your blog have a contact page? I'm having
    a tough time locating it but, I'd like to shoot you
    an e-mail. I've got some creative ideas for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it develop over time.


    Also visit my weblog: Louis Vuitton Sale

    ReplyDelete
  22. Heya just wanted to give you a quick heads up and let you know a few of the images aren't loading properly.
    I'm not sure why but I think its a linking issue.
    I've tried it in two different internet browsers and
    both show the same results.

    my website :: Beats By Dre Studio

    ReplyDelete
  23. I really like looking through a post that can make people think.
    Also, thank you for allowing for me to comment!


    My site :: Christian Louboutin Sydney

    ReplyDelete
  24. Simply wish to say your article is as amazing. The clearness in your post
    is simply nice and i could assume you are an expert on this subject.

    Well with your permission let me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please keep up the gratifying work.


    Feel free to visit my weblog; New Balance Discount

    ReplyDelete