Вы находитесь на странице: 1из 72

UNIT-5

Web services,
JAX-RPC,
WSDL,
XML Schema,
and SOAP.

1
Web service
A Web service is a software system
designed to support interoperable
machine-to-machine interaction over a
network.
It has an interface described in a
machine-processable format (specifically
WSDL).
Other systems interact with the Web
service in a manner prescribed by its
description using SOAP messages.
2
Definition
A web service is a collection of open protocols and
standards used for exchanging data between
applications or systems.

Software applications written in various programming


languages and running on various platforms can use
web services to exchange data over computer networks
like the Internet. This interoperability (e.g., between
Java and Python, or Windows and Linux applications) is
due to the use of open standards.

Ex : ATM machine

3
Web service..
A web service is a piece of business
logic, located somewhere on the Internet,
that is accessible through standard-based
Internet protocols such as HTTP or
SMTP.
Web Services allows to expose the
functionality of existing code over the
network. Once it is exposed on the
network, other application can use the
functionality of program
4
Benefits or characteristics of Web
Services:
Exposing the function on to network:
A Web service is a unit of managed code that can be remotely invoked
using HTTP, that is, it can be activated using HTTP requests. So,
Web Services allows you to expose the functionality of your existing
code over the network. Once it is exposed on the network, other
application can use the functionality of your program.
Connecting Different Applications:
Web Services allows different applications to talk to each other and
share data and services among themselves. Other applications can
also use the services of the web services. For example VB or .NET
application can talk to java web services and vice versa. So, Web
services is used to make the application platform and technology
independent.
Standardized Protocol:
Web Services uses standardized industry standard protocol for the
communication. All the four layers (Service Transport, XML
Messaging, Service Description and Service Discovery layers) uses
the well defined protocol in the Web Services protocol stack. This
standardization of protocol stack gives the business many advantages
like wide range of choices, reduction in the cost due to competition
and increase in the quality.
5
Benefits or characteristics of Web
Services:
Web Services Sharing:
These days due to complexness of the business, organizations
are using different technologies like EAI, EDI, B2B, Portals etc.
for distributing computing. Web Services supports all these
technologies, thus helping the business to use existing
investments in other technologies.
Web Services are Self Describing:
Web Services are self describing applications, which reduces
the software development time. This helps the other business
partners to quickly develop application and start doing
business. This helps business to save time and money by
cutting development time.
Automatic Discovery:
Web Services automatic discovery mechanism helps the
business to easy find the Service Providers. This also helps
your customer to find your services easily. With the help of Web
Services your business can also increase revenue by exposing
their own Web Services available to others.
6
Components of Web Services?

The basic Web services platform is XML


+ HTTP. All the standard Web Services
works using following components
SOAP (Simple Object Access Protocol)
UDDI (Universal Description, Discovery and
Integration)
WSDL (Web Services Description Language)

7
Web Services
Web Services = WSDL + SOAP + UDDI

What is SOAP?
SOAP is an XML-based protocol to let
applications exchange information over HTTP.
SOAP is a protocol for accessing a Web Service.
SOAP stands for Simple Object Access Protocol
SOAP is a communication protocol
SOAP is a format for sending messages
SOAP is designed to communicate via Internet
SOAP is platform independent
SOAP is language independent
SOAP is based on XML
8
Web Services..
What is WSDL?
WSDL is an XML-based language for
locating and describing Web services.
WSDL stands for Web Services Definition
Language
WSDL is based on XML
WSDL is used to describe Web services
WSDL is used to locate Web services

9
Web Services..
What is UDDI?
UDDI is a directory service where companies
can register and search for Web services.
UDDI stands for Universal Description, Discovery
and Integration
UDDI is a directory for storing information about web
services
UDDI is a directory of web service interfaces
described by WSDL
UDDI communicates via SOAP
UDDI is built into the Microsoft .NET platform

10
Web Services Architecture

11
Web Service Roles
There are three major roles within the web
service architecture:
Service provider: This is the provider of the web
service. The service provider implements the service
and makes it available on the Internet.

Service requestor This is any consumer of the web


service. The requestor utilizes an existing web
service by opening a network connection and
sending an XML request.

Service registry This is a logically centralized


directory of services. The registry provides a central
place where developers can publish new services or
find existing ones. It therefore serves as a
centralized clearinghouse for companies and their
services.
12
Web Service Protocol Stack
The web service architecture is to examine the emerging web
service protocol stack. The stack is still evolving, but currently has
four main layers.
Service transport This layer is responsible for transporting messages
between applications. Currently, this layer includes hypertext transfer
protocol (HTTP), Simple Mail Transfer Protocol (SMTP), file transfer
protocol (FTP), and newer protocols, such as Blocks Extensible
Exchange Protocol (BEEP).
The bottom of the web service protocol stack is service transport. This
layer is responsible for actually transporting XML messages between
two computers.
Hyper Text Transfer Protocol (HTTP) Currently, HTTP is the most
popular option for service transport. HTTP is simple, stable, and widely
deployed. Furthermore, most firewalls allow HTTP traffic. This allows
XMLRPC or SOAP messages to masquerade as HTTP messages. This is
good if you want to easily integrate remote applications, but it does raise a
number of security concerns.

Blocks Extensible Exchange Protocol (BEPP) One promising alternative


to HTTP is the Blocks Extensible Exchange Protocol (BEEP).BEEP is a
new IETF framework of best practices for building new protocols. BEEP is
layered directly on TCP and includes a number of built-in features,
including an initial handshake protocol, authentication, security, and error
handling. Using BEEP, one can create new protocols for a variety of
applications, including instant messaging, file transfer, content syndication,
and network management 13
XML messaging This layer is responsible for
encoding messages in a common XML format so
that messages can be understood at either end.
Currently, this layer includes XML-RPC and SOAP.

Service description This layer is responsible for


describing the public interface to a specific web
service. Currently, service description is handled via
the Web Service Description Language (WSDL).

Service discovery This layer is responsible for


centralizing services into a common registry, and
providing easy publish/find functionality. Currently,
service discovery is handled via Universal
Description, Discovery, and Integration (UDDI).

14
XML Messaging Using SOAP

15
NDFD/java interface
package NDFD;
public interface NdfdXMLPorttype extends Remote{
Public String NDFDgen(BigDecimal
latitude,BigDecimal longitude)throws RemoteException;

NDFD/java class implementing the interface


package NDFD;
public class weatherimpl
implements NdfdXMLPorttype {
public String NDFDgen(BigDecimal latitude,
BigDecimal,longitude) {
return message;
}

}
16
Accessing Java web service
// create an object representing the web service

NdfdXMLPorttype ndfdws=new
NdfdXML_Impl().getNdfdXMLPort()

//request a forecast from the web service

BigDecimal latitude= new BigDecimal (40.28);


BigDecimal longitude = new BigDecimal (50.28);
String
forcast=ndfdws.NDFDgen(latitude,longitude);

NdfdXMLPorttype and NdfdXML_Impl class were


automatically generated by wscompile from the nws WSDL 17
Packaging Soap
HTTP Post

SOAP Envelope

SOAP Head

SOAP Body

18
HTTP request embedded SOAPdocument
POST/forcast/XML/SOAP_server/NdfdXMLPorttype http/1.1
Content type:txt/html:charset=utf-8

<?xml version='1.0' encoding='UTF-8'?>


<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Header>
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<nsO:NDFDgen>
<latitude xsi: type=xsd:decimal>40.28</latitude>
<longitude xsi: type=xsd:decimal>50.28</longitude>
..

</SOAP-ENV:Body>
</SOAP-ENV:Envelope> 19
HTTP response embedded SOAPdocument
http/1.1 200 ok server:apache/2.0.46 (Red hat)

<?xml version='1.0' encoding='UTF-8'?>


<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Header>
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<NDFDgenresponse>
<xmlOut xsi:type=xsd:string>
Maximum temperature value&gt :47/ value&gt
</XMLOut>
</NDFDgenresponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
20
<?xml version='1.0' encoding='UTF-8' >
<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
..
<message name="NDFDgenrequest ">
<part name="latitude " type =xsd:decimal />
<part name="longiitude " type =xsd:decimal />
</message>
<message name=" NDFDgenResponse">
<part name="XMLOut " " type =xsd:string" />
</message>
---
<portType name="DFDXMLPorttype ">
<operation name="NDFDgen ">
WSDL Document
<input message=tns: NDFDgenrequest " />
<output message="tns: NDFDgenResponse " />
</operation>
</portType>
<service name="nws ">
<port name="ndfdPort" binding="tns:ndfd PortBinding">
<soap:address location="http://localhost:8080/ndfd/ndfdService" />
</port>
</service>
</definitions> 21
JAX-WS Maps WSDL to a Java API

package samples; <definitions ...


targetNamespace="http://samples/"
import javax.jws.WebService; name="HelloService">
<types> ... </types>
@WebService <message name="sayHello"> ... </message>
public class Hello { <portType name="Hello">
<operation name="sayHello">
public String sayHello(String s) { <input message="tns:sayHello"/>
return "Hello: " + s; <output message="tns:sayHelloResponse"/>
} </operation>
</portType>
} <binding name="HelloPortBinding"
type="tns:Hello">
<soap:binding ... />
<operation name="sayHello"> ...
</operation>
</binding>
<service name="HelloService">
<port name="HelloPort ...
</port>
</service>
</definitions>

22
9.2 writing a Java web service
Currency conversion service
To perform conversion between different
currencies.
The web service will return the equivalent
amount of other two currencies
The web service provide 3 operations
Fromdollers,fromeuros,fromyens

23
Writing server software
To built the web service using 2 tools
Wscompile and wsdeploy
The webservice consists of an interface and a
class to implements this interface.
After writing this code,we run wscompile to
convert java files into WSDL document.
Then run wsdeploy to package webservice.
Working directories
currencyConverter
WEB-INF
Src
Mycurcon
classes
24
The starting point of web service is writing
java interface which provide the
operations. This is known as service end
point interface.
Basic rules for service end point interface
is
The interface must extend the
java.rmi.Remote interface
Every method in the interface must throws
RemoteExeption
The interface must not contain any public
static final declarations

25
Class exchangevalues
The class exchangevalues representing the
return value from operations of the currency
converter.

Package mycurCon
Public class exchangevalues{
public double dollars;
Public double euros;
Public double yen;
}
26
Service end point interface

Package mycurCon
Public interface curcon extends java.rmi.Remote{
public exchangevalues fromdollars(double dollars)
throws RemoteException
public exchangevalues fromEuros(double euros)
throws RemoteException;
public exchangevalues fromyen(double yen) throws
RemoteException

}
27
implementation
Public exchangevalues fromdollars(double
dollars) throws RemoteException
{
exchangevalues Ev=new exchangevalues();
Ev.dollars=dollars;
Ev.euros=dollars*dollars2euro;
Ev.yen=dollars * dollars2yen;
return Ev;
}
28
Packaging server software
Java d classes src/mycurCon/*.java

Wscompile to create the WSDL document for theweb


service.

Wscompile define d WEB-INF classpath WEB-


INF/classes model WEB-INF/model.xml.gz config.xml

d option specifies the directory to receive the generated


WSDL document.

29
config.xml
The config.xml file consists of a series of XML
elements. The Domain element is the top-level element,
and all elements in the Domain are children of the
Domain element.
The Domain element includes child elements, such as

Server, Cluster, and Application elements. These child


elements may have children themselves.
For example, the Server element includes the child elements
WebServer, SSL and Log.
The Application element includes the child elements
EJBComponent and WebAppComponent.
Each element has one or more configurable attributes. An
attribute has a corresponding attribute in the configuration API.
30
Model xml document-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<configuration
xmlns=http://java.sun.com/xml/ns/jax-rpc/ri/config>
<service
name=historiccurrencyconverter
targetnamespace=http://temp-uri-org/wsdl
typenamespace= http://emp-uri-org/types
packagename=mycurCon
<interface name =mycurCon.curcon/>
</service>

</ configuration>

The input to wscomplie are supplied via xml document.

The purpose of the config.xml file is to provide configuration data to each of the
blocks that require configuration data.

31
Another XML configuration file jaxrpc-ri.xml
<?xml version='1.0' encoding='UTF-8'?>
<webservices
xmlns=http://java.sun.com/xml/ns/jax-rpc/ri/dd
version=1.0
targetnamespaceBase=http://temp-uri-org/wsdl
typenamespaceBase= http://emp-uri-org/types
urlpatternBase=/converter >
<endpoint
name=currencyconverter
displayName=currency Converter
description=converts between dollars,euros and yen
interface name =mycurCon.curcon
Model=WEB-INF/model.xml.gz
Implementation=mycurCon.curconimpl />
<endpointMapping
endpointName=currencyconverter
urlpattern=/currency />
</webservices>

Endpoint provide information to web server such as


name of the service, implementation,location of the model.xml
url-pattern specify the path of the webservice
32
Web.xml
The web.xml file provides configuration and deployment information for
the Web components that comprise a Web application.

<web-app
xmlns=http://java.sun.com/xml/ns/j2ee
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemalocation=http://java.sun.com/xml/ns/j2ee/web-app-
2_4.xsd
version=2.4 >
<displayName> Historic currency converter</displayName>
<description>
----
</description>

Jaxrpc-ri.xml supplies any mapping needed to web.xml

33
wsdeploy
Jar cf converter-temp.war WEB-INF

wsdeploy o converter.war converter-temp.war

Finally copied converter.war into webapp directory

34
9.3 writing a java web service client
XML document for wscompile

<?xml version='1.0' encoding='UTF-8'?>


<configuration
xmlns=http://java.sun.com/xml/ns/jax-rpc/ri/config>
<wsdl
location=http://localhost:8084/converter/currency?WSDL
packagename=mycurConclient />

</ configuration>

wscompile gen keep d classes s src config.xml


it generate class files from the WSDL specified in config.xml

Directory structure like this


Webapp
converterclient
WEB-INF
classes
mycurconclient
src 35
mycurconclient
The object of type curcon returned by getcurconport() method of an
instance of this classes is a proxy object representing the web
service.

Curcon cobj=(new Historiccurrencyconverter_Impl()).getcurconport()

The interface generated by wscompile is shared between web server


and clients via the wsdl

Package mycurconclient
Public interface historiccurrencyconverter extends rpc.Service
{
public mycurconlient.curcon.getcurconport();
}

36
Package mycurConclient
Public interface curcon extends java.rmi.Remote{

public mycurConclient . exchangevalues fromdollars(double dollars)


throws RemoteException

public mycurConclient.exchangevalues fromEuros(double euros) throws


RemoteException;

public mycurConclient.exchangevalues fromyen(double yen) throws RemoteException


}

Package mycurCon
Public class exchangevalues{
protected double dollars;
protected double euros;
protected double yen;
Public double getdollars() {
return dollars; }

Public double geteuros() {


return euros; }

Public double getyen() {


return yen; }
} 37
Curconbean for accessing the web service
Package mycurConclient
Public class curconbean{
Private double value=1.0;
Private string currency=dollars;
Public void setvalue(double value){
this.value=value;
return;
}
Public void setcurrency(String currency){
this.currency=currency;
return;
}
Public exchangevalue getexvalue()
{
exchangevalue ev=null;
Curcon cobj=(new Historiccurrencyconverter_Impl()).getcurconport()
if(currency.equals(euros)) {

ev.curcon.fromeuros(value); }
else if(currency.equals(yen)) {

ev.curcon.fromeyen(value);
else {
38
ev.curcon.fromedollars(value); } return ev; }
Wsdl-1st set
<?xml version='1.0' encoding='UTF-8'>
<definitions
name="historiccurrencyconverter "
targetnamespace=http://tempuri-org/wsdl
xmlns:tns="http://tempuri-org/wsdl
xmlns:ns2=" http://tempuri-org/wsdl/types "
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" >

default name space for wsdl- http://schemas.xmlsoap.org/wsdl


xsd and soap are associated with xml schema namespace

39
Wsdl- 2 nd set
<types>
<xsd:schema>
targetnamespace=http://tempuri-org/types
xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/
xmlns="http://www.w3.org/2001/XMLSchema"
<xsd:import namespace="http:// historiccurrencyconverter/"
schemaLocation="http://localhost:8080/ currencyconverterWebService/
currencyconverterWebServiceService?xsd=1" />
<complextype name =exchangevalue>
<sequence>
<element name=dollars type=double />
<element name=euros type=double />
<element name=yen type=double />
</sequence>
</complextype>
</xsd:schema>
</types>

Types element define the data type that can be used as a input parameter or
return value of wsdl document

40
Wsdl- 3 rd set
<message name=" curcon_fromdollars ">
<part name="double1 " type="xsd:double " />
</message>

<message name=" curcon_fromdollars Response">


<part name="result " element="ns2:exchangevalues " />
</message>

<message name=" curcon_fromeuros ">


<part name="double1 " type="xsd:double " />
</message>

<message name=" curcon_fromeurosResponse">


<part name="result " element="ns2:exchangevalues " />
</message>

<message name=" curcon_fromyen ">


<part name="double1 " type="xsd:double " />
</message>

<message name=" curcon_fromyenResponse">


<part name="result " element="ns2:exchangevalues " />
</message>
41
Wsdl- 4th set
<portType name="curcon ">
<operation name="fromdollars " paramerterorder=double1>
<input message="tns:curcon_fromdollars " />
<output message="tns: curcon_fromdollarsResponse" />
</operation>
<operation name="fromeuros " paramerterorder=double1>
<input message="tns:curcon_fromeuros " />
<output message="tns: curcon_fromeurosResponse" />
</operation>
<operation name="fromyen " paramerterorder=double1>
<input message="tns:curcon_fromyen " />
<output message="tns: curcon_fromyenResponse" />
</operation>
</portType>

42
Wsdl-5 th set
<binding name="curconBinding" type="tns:curcon">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name=" fromdollars ">
<soap:operation soapAction="" />
<input>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</input>
<output>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</output>
</operation>
<operation name=" fromeuros ">
<soap:operation soapAction="" />
<input>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</input>
<output>
<soap:body
Encodingstyle= http://schemas.xmlsoap.org/soap/encoding
use="encoded" Namespace= http://tempuri-org/wsdl />
</output> 43
</operation>
</binding>
Final set
<service name=" historiccurrencyconverter ">
<port name="curconPort" binding="tns:curconBinding">
<soap:address location="http://localhost:8080/
currencyconverterWebService / currencyconverterWebService
Service" />
</port>
</service>
</definitions>

44
XML Schemas
Schemas is a general term--DTDs are a form of XML
schemas
According to the dictionary, a schema is a
structured framework or plan
When we say XML Schemas, we usually mean the
W3C XML Schema Language
This is also known as XML Schema Definition
language, or XSD
DTDs, XML Schemas, and RELAX NG are all XML schema
languages

45
Why XML Schemas?
DTDs provide a very weak specification language
cant put any restrictions on text content
very little control over mixed content (text plus elements)
little control over ordering of elements
DTDs are written in a strange (non-XML) format
need separate parsers for DTDs and XML
The XML Schema Definition language solves these problems
XSD gives much more control over structure and content
XSD is written in XML

46
Referring to a schema
To refer to a DTD in an XML document, the reference
goes before the root element:
<?xml version="1.0"?>
<!DOCTYPE rootElement SYSTEM "url">
<rootElement> ... </rootElement>
To refer to an XML Schema in an XML document, the
reference goes in the root element:
<?xml version="1.0"?>
<rootElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
(The XML Schema Instance reference is required)
xsi:noNamespaceSchemaLocation="url.xsd">
(This is where your XML Schema definition can be
found)
... 47
</rootElement>
The XSD document
Since the XSD is written in XML
Except for the additions to the root element of
XML data document,
The file extension is .xsd
The root element is <schema>
The XSD starts like this:
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.rg/2001/XMLSchema">

48
<schema>
The <schema> element may have
attributes:
xmlns:xs="http://www.w3.org/2001/XMLS
chema"
This is necessary to specify where all our XSD
tags are defined
elementFormDefault="qualified"
This means that all XML elements must be
qualified

49
Simple and complex elements
A simple element is one that contains text and
nothing else
A simple element cannot have attributes
A simple element cannot contain other elements
A simple element cannot be empty
However, the text can be of many different types,
and may have various restrictions applied to it
If an element isnt simple, its complex
A complex element may have attributes
A complex element may be empty, or it may
contain text, other elements, or both text and other
elements
50
User defined simple type
A simple element is defined as
<xs:element name="name" type="type" />
where:
name is the name of the element
the most common values for type are
xs:boolean xs:integer
xs:date xs:string
xs:decimal xs:time
Other attributes a simple element may have:
default="default value" if no other value is specified
fixed="value" no other value may be
specified

51
Defining an attribute
Attributes themselves are always declared as simple types
An attribute is defined as
<xs:attribute name="name" type="type" />
where:
name and type are the same as for xs:element
Other attributes a simple element may have:
default="default value" if no other value is specified
fixed="value" no other value may be specified
use="optional" the attribute is not required
(default)
use="required" the attribute must be present
52
Restrictions, or facets
The general form for putting a restriction on a text value is:
<xs:element name="name"> (or xs:attribute)
<xs:restriction base="type">
... the restrictions ...
</xs:restriction>
</xs:element>
For example:
<xs:element name=priorityType">
<xs:restriction base="xs:integer">
<xs:minInclusive value=10">
<xs:maxInclusive value="140">
</xs:restriction>
</xs:element>

53
Restrictions on numbers
minInclusive -- number must be the given value
minExclusive -- number must be > the given value
maxInclusive -- number must be the given value
maxExclusive -- number must be < the given value
totalDigits -- number must have exactly value digits
fractionDigits -- number must have no more than
value digits after the decimal point

54
Restrictions on strings
length -- the string must contain exactly value characters
minLength -- the string must contain at least value characters
maxLength -- the string must contain no more than value characters
pattern -- the value is a regular expression that the string must match
whiteSpace -- not really a restriction--tells what to do with whitespace
value="preserve" Keep all whitespace
value="replace" Change all whitespace characters to spaces
value="collapse" Remove leading and trailing whitespace, and replace
all sequences of whitespace with a single space

55
Enumeration
An enumeration restricts the value to be one of a
fixed set of values
Example:
<xs:element name="season">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Spring"/>
<xs:enumeration value="Summer"/>
<xs:enumeration value="Autumn"/>
<xs:enumeration value="Fall"/>
<xs:enumeration value="Winter"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
56
User defined Complex Type
A complex element is defined as
<xs:element name="name">
<xs:complexType>
... information about the complex type...
</xs:complexType>
</xs:element>
Example:
<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>
<xs:sequence> says that elements must occur in this order
Remember that attributes are always simple types
57
Global and local definitions
Elements declared at the top level of a <schema> are available
for use throughout the schema
Elements declared within a xs:complexType are local to that
type
Thus, in
<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>
the elements firstName and lastName are only locally declared
The order of declarations at the top level of a <schema> do not
specify the order in the XML data document 58
Declaration and use
So far weve been talking about how to declare types,
not how to use them
To use a type we have declared, use it as the value
of type="..."
Examples:
<xs:element name="student" type="person"/>
<xs:element name="professor" type="person"/>
Scope is important: you cannot use a type if is
local to some other type

59
xs:sequence
Weve already seen an example of a complex type
whose elements must occur 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>
60
xs:all
xs:all allows elements to appear in any order
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstName" type="xs:string" />
<xs:element name="lastName" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
Despite the name, the members of an xs:all
group can occur once or not at all
You can use minOccurs="n" and maxOccurs="n" to
specify how many times an element may
occur (default value is 1)
In this context, n may only be 0 or 1 61
Referencing
Once you have defined an element or attribute (with
name="..."), you can refer to it with ref="..."
Example:
<xs:element name="person">
<xs:complexType>
<xs:all>
<xs:element name="firstName"
type="xs:string" />
<xs:element name="lastName"
type="xs:string" />
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="student" ref="person">
Or just: <xs:element ref="person">
62
SOAP
SOAP is a simple, lightweight XML
protocol for exchanging structured and
typed information on the Web

63
SOAP Contains Four Parts:
An extensible envelope expressing (mandatory)
what features and services are represented in a message;
who should deal with them,
whether they are optional or mandatory.
A set of encoding rules for data (optional)
Exchange instances of application-defined data types and
directed graphs
Uniform model for serializing abstract data models that can not
directly be expressed in XML schema
A Convention for representation RPC (optional)
How to make calls and responses
A protocol binding to HTTP and HTTP-EF (optional)

64
Packaging Soap
HTTP Post

SOAP Envelope

SOAP Head

SOAP Body

65
SOAP Example in HTTP
POST /Accounts/Henrik HTTP/1.1 SOAP-HTTP Binding
Host: www.webservicebank.com HTTP Request
Content-Length: nnnn
Content-Type: text/xml; charset="utf-8"
SOAP Body
SOAPAction: "Some-URI" SOAP Header
SOAP Envelope
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/"
SOAP:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP:Header>
<t:Transaction xmlns:t="some-URI" SOAP:mustUnderstand="1">
5
</t:Transaction>
</SOAP:Header>
<SOAP:Body>
<m:Deposit xmlns:m="Some-URI">
<m:amount>200</m:amount>
</m:Deposit>
</SOAP:Body>
66
</SOAP:Envelope>
Rpc representation
Style attribute of soap:binding element in wsdl
expected to communicate with clients in an rpc
style.
Soap:body indicated using an encoding
specified by soap1.1 to represent the RPC
The soap specified rpc calls is a modeled as a
form of data structure known as struct.(instance
of class)
From an xml point of view , the type of data
structure defined by a complex data type is a
struct.
67
Soap document sent by client to currency converter

<?xml version='1.0' encoding='UTF-8'>


<env:Envelope
xmlns:env=http://schemas.xmlsoap.org/soap/envelope
xmlns:xsd=http://www.w3.org/2001/XMLSchema
xmlns:xsi=http://www.w3.org/2001/XMLSchema/instance
xmlns:enc=http://schemas.xmlsoap.org/soap/encoding
xmlns:ns0=http://tempuri-org/wsdl
xmlns:ns1=" http://tempuri-org/wsdl/types "
xmlns:encdingstyle=http://schemas.xmlsoap.org/soap/encoding />

<env:body>
<ns0:fromdollars>
<double1 xsi:type=xsd:double>1.0</double1>
</ns0:fromdollars>
</env:body>
</env:Envelope>

68
Soap document sent by web service to client
<?xml version='1.0' encoding='UTF-8'>
<env:Envelope
xmlns:env=http://schemas.xmlsoap.org/soap/envelope
xmlns:xsd=http://www.w3.org/2001/XMLSchema
xmlns:xsi=http://www.w3.org/2001/XMLSchema/instance
xmlns:enc=http://schemas.xmlsoap.org/soap/encoding
xmlns:ns0=http://tempuri-org/wsdl
xmlns:ns1=" http://tempuri-org/wsdl/types "
xmlns:encdingstyle=http://schemas.xmlsoap.org/soap/encoding />

<env:body>
<ans1:fromdollarsResponse
xmlns:ans1=http://tempuri.org/wsdl>
<result href=#id1/>
</ans1:fromdollarsResponse>

<ns0:exchangevalues id=#id1 xsi:type=ns0:exchangevalues>


<dollars xsi:type=xsd:double>1.0</dollars>
<euros xsi:type=xsd:double>0.765</euros>
<yen xsi:type=xsd:double>102.45</yen>

</ ns0:exchangevalues >


</env:body>
</env:Envelope>

69
Soap encoding of structure data
Soap defines a simple but effective mechanism for
representing struct data, whether it be a struct
representing an RPC call or response or parameter or
return value.
<env:body>
<ans1:fromdollarsResponse
xmlns:ans1=http://tempuri.org/wsdl>
<result href=#id1/>
</ans1:fromdollarsResponse>

<ns0:exchangevalues id=#id1 xsi:type=ns0:exchangevalues>


<dollars xsi:type=xsd:double>1.0</dollars>
<euros xsi:type=xsd:double>0.765</euros>
<yen xsi:type=xsd:double>102.45</yen>

</ ns0:exchangevalues >


</env:body>

70
Soap encoding of array
<import
Namespace=http://schemas.xmlsoap.org/soap/en
coding//>
<complextype name =arrayofdouble>
<complexcontent>
<restriction base=soap11-enc:Array>
<attribute ref= soap11-enc:Arraytype
wsdl:arrayType=double[] />
<element name=yen type=double />
</ restriction >
</complexcontent>
</complextype>
71
Soap response document
<env:body>
<ans1:fromdollarsResponse
xmlns:ans1=http://tempuri.org/wsdl>
<result href=#id1/>
</ans1:fromdollarsResponse>

<ns0:exarrayofsouble id=#id1
xsi:type=enc:array
enc:arrayType=xsd:double[3]>

<item xsi:type=xsd:double>1.0</ item >


< item xsi:type=xsd:double>0.765</ item >
< item xsi:type=xsd:double>102.45</ item >

</ <ns0:exarrayofsouble >


</env:body>
72

Вам также может понравиться