0% found this document useful (0 votes)
2 views

Ch-2 -- Defining SOAP Messages with WSDL (1)

Uploaded by

miki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ch-2 -- Defining SOAP Messages with WSDL (1)

Uploaded by

miki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Chapter 2: Defining SOAP Messages with WSDL

Contents:
• XML essentials
o Introduction to XML
o DTD vs. XSD
o What is XML Parsing?
o Various other XML Binding API’s
o XML namespaces
o Describing XML with schema
• Structure of SOAP messages
o Role of SOAP in web services
o Operations, messages and faults
• Anatomy of a WSDL document
o Defining the interfaces of a web service
o Specifying implementation
1
XML essentials

• XML stands for Extensible Markup Language


• XML is a markup language much like HTML
• XML was designed to store and transport data
• XML is a software- and hardware-independent tool
• XML is used to define rules for encoding documents in a format that is both human-
readable and machine-readable.
• It is not a programming language but a set of rules for structuring data in a platform-
neutral format.
• It’s widely used in web services to structure data, making it easy to transmit between
2
different systems.
Characteristics of XML
Self-Descriptive – XML uses tags to define data and its structure in a human-readable
format.
Extensible – Users can create their own custom tags based on the needs of their
applications.
Hierarchical Structure – Data is stored in a tree-like format, making it easy to organize
and access.
Platform-Independent – XML can be used across different software, hardware, and
platforms.
Interoperability – XML is widely used for data exchange between different applications
and systems. 3
XML Elements

• Elements can behave as containers to hold text, elements, attributes, media objects or
all of these.
• Each XML document contains one or more elements, the scope of which are either
delimited by start and end tags, or for empty elements, by an empty-element tag.
<?xml version = "1.0"?>
<contact-info> Element

<address>
Syntax:
<name>ABC</name>
<element-name> <company>XYZ</company>
....content <phone>123-4567</phone>
</element- </address>
4
</contact-info>
XML Attributes
• XML Attributes: are used to distinguish among elements of the same name, when you
do not want to create a new element for every situation.
• Attributes are designed to contain data related to a specific element.
• Attributes cannot contain multiple values (elements can)

Example:
Attribute
Syntax: <garden>
<element-name attribute1 <plants category =
attribute2 > "flowers" />
....content.. <plants category = "shrubs">
< /element-name> </plants> 5
XML Syntax Rules
• 1. XML Documents Must Have a Root Element
• XML documents must contain one root element that is the parent of all other
elements:
• Example <root>
<child>
<subchild>.....</subchild>
</child>
</root>
• Example <note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
• In this example <note> is the root element:
The XML Prolog (XML Declaration)

• This line is called the XML prolog:

• <?xml version="1.0" encoding="UTF-8"?>

• UTF-8 is the default character encoding for XML documents.

2. All XML Elements Must Have a Closing Tag

• In XML, it is illegal to omit the closing tag.

• <p>This is a paragraph.</p>
3. XML Tags are Case Sensitive

• Opening and closing tags must be written with the same case:

• <Message>This is incorrect</message>
<message>This is correct</message>

4. XML Elements Must be Properly Nested

• All elements must be properly nested within each other:


• <b><i>This text is bold and italic</i></b>
5. XML Attribute Values Must be quoted

• INCORRECT: <note date=12/11/2007>


</note>

• CORRECT: <note date="12/11/2007">


</note>
Entity References

• Some characters have a special meaning in XML.

• Example: "<" inside an XML element.

• <message>salary < 1000</message>

• This will generate an XML error:

• To avoid this error, replace the "<" character with an entity reference:
<message>salary &lt; 1000</message>
There are 5 pre-defined entity references in XML:

&lt; < less than


&gt; > greater than

&amp; & ampersand

&apos; ' Apostrophe

&quot; " quotation mark


Comments in XML

• The syntax for writing comments in XML is similar to that of HTML.

• <!-- This is a comment -->

• Two dashes in the middle of a comment are not allowed.

• <!-- This is a -- comment -->

• White-space is Preserved in XML.

• XML does not truncate multiple white-spaces (HTML truncates multiple white-
spaces to one single white-space):
XML Naming Rules

• XML elements must follow these naming rules:


• Element names are case-sensitive

• Element names must start with a letter or underscore

• Element names cannot start with the letters xml (or XML, or Xml, etc)

• Element names can contain letters, digits, hyphens, underscores, and periods

• Element names cannot contain spaces

• Any name can be used, no words are reserved (except xml).


Best Naming Practices
• Create descriptive names, like this: <person>, <firstname>, <lastname>.

• Create short and simple names, like this: <book_title> not like this:
<the_title_of_the_book>.

• Avoid "-". If you name something "first-name", some software may think you want to
subtract "name" from "first".

• Avoid ".". If you name something "first.name", some software may think that "name" is a
property of the object "first".

• Avoid ":". Colons are reserved for namespaces (more later).

• Non-English letters like éòá are perfectly legal in XML, but watch out for problems if
your software doesn't support them.
XML Namespaces
XML Namespaces provide a method to avoid element name conflicts.
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict
when trying to mix XML documents from different XML applications.

• This XML carries HTML table • This XML carries information about a
information: table (a piece of furniture):
• <table> • <table>
<tr> <name>African Coffee Table</name>
<td>Apples</td> <width>80</width>
<td>Bananas</td> <length>120</length>
</tr> </table>
</table>
If these XML fragments were added together, there would be a name conflict. Both contain
a <table> element, but the elements have different content and meaning.
Solving the Name Conflict Using a Prefix

This XML carries information about an HTML table, and a piece of furniture:

• Example <f:table>
• <h:table> <f:name>African Coffee
<h:tr> Table</f:name>
<h:td>Apples</h:td> <f:width>80</f:width>
<h:td>Bananas</h:td> <f:length>120</f:length>
</h:tr> </f:table>
</h:table>
XML Tree
The image above represents books in this XML:
• <book category="children">
• <?xml version="1.0“ encoding="UTF-8"?> <title lang="en">Harry Potter</title>
<bookstore> <author>J K. Rowling</author>
<book category="cooking"> <year>2005</year>
<title lang="en">Everyday Italian</title> <price>29.99</price>
<author>Giada De Laurentiis</author> </book>
<year>2005</year> <book category="web">
<price>30.00</price> <title lang="en">Learning XML</title>
</book> <author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Valid XML Documents

• A "well formed" XML document is not the same as a "valid" XML


document.

• An XML document with correct syntax is called "Well Formed".

• A "Valid" XML document is a "Well Formed" XML document, which also


conforms to the rules of a DTD

• Even if documents are well-formed they can still contain errors


DTD vs. XSD
• There are two different document type definitions that can be used with XML:
DTD - The Document Type Definition
XML Schema - An XML-based alternative to DTD
• Both mechanisms for defining the structure and constraints of XML documents.
• However, they differ in complexity and capabilities.

20
DTD (Document Type Definition):
• What is a DTD?
• A DTD defines the structure and the legal elements and attributes of an
XML document.
• With a DTD, independent groups of people can agree on a standard DTD for
interchanging data.
• An application can use a DTD to verify that XML data is valid.
• An XML document validated against a DTD is both "Well Formed" and
"Valid".
• Limitations: Does not support data types beyond basic types and cannot
enforce data validation as rigorously as XSD.
Types of DTD

• DTD can be classified on its declaration basis in the XML document, such as:

 Internal DTD

• When a DTD is declared within the file it is called Internal DTD and

• To reference it as internal DTD, standalone attribute in XML declaration must be


set to yes. This means the declaration works independent of external source.

 External DTD

• if a DTD is declared in a separate file it is called External DTD.

• To reference it as external DTD, standalone attribute in the XML declaration must


be set as no.
Syntax of internal DTD
• <!DOCTYPE root-element [element-declarations]>
• XML document with an internal DTD
• <?xml version="1.0" standalone="yes" >
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Syntax of external DTD

• <?xml version="1.0"? standalone="no" >


<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
• And here is the file "note.dtd", which contains the DTD:
• <!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
• ]>
DTD – ATTRIBUTES

• Attribute gives more information about an element or more precisely it defines a


property of an element.

• An XML attribute is always in the form of a name-value pair.

• An element can have any number of unique attributes.

• Syntax

• <!ATTLIST element-name attribute-name attribute-type attribute-value>


Example
<?xml version="1.0"?>
<!DOCTYPE address [
<!ELEMENT address (company)* >
<!ELEMENT company (#PCDATA)>
<!ATTLIST company name CDATA #REQUIRED>
]>
<address>
<company name=“ABC">we are ABC company </company>
</address>
XSD (XML Schema Definition):
• It is more powerful than DTD
• XSD provides a way to define the structure and data types of XML documents.
• Data Types: Supports a wide range of data types (e.g., string, integer, date) and can
enforce data type constraints on elements.
• Namespace Support: Used to avoid naming conflicts by differentiating elements with
the same name from different sources.
• XML Schema is significantly more complicated than DTDs.

27
XSD (XML Schema Definition):
 Defining Element
<xs:element name="xxx" type="yyy"/>
where xxx is the name of the element and yyy is the data type of the element
.Example: Here are some XML elements:
<lastname>Alex</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
• And here are the corresponding simple element definitions:
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/> 28
XSD (XML Schema Definition):
 Define an Attribute
<xs:attribute name="xxx" type="yyy"/>
• where xxx is the name of the attribute and yyy specifies the data type of the attribute.
Example
• Here is an XML element with an attribute:
<lastname lang="EN">Smith</lastname>
• And here is the corresponding attribute definition:
<xs:attribute name="lang" type="xs:string"/>

29
XSD (XML Schema Definition):
 Simple Element VS Complex Element
1. Simple Elements
• Can contain only text values (no attributes or child elements).
• Do not have nested elements.
2. Complex Elements
• Can contain attributes, child elements, or both.
• Can be structured with sequences, choices, and groups.

30
XSD (XML Schema Definition):
XML
<person id="101">
<firstname>John</firstname>
<lastname>Doe</lastname>
</person>
XSD
<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:attribute name="id" type="xs:int"/>
</xs:complexType>
</xs:element> 31
Cont’d…
Comparison:
• Expressiveness: XSD is more expressive than DTD, allowing for greater complexity
and data validation.
• Validation: XSD can provide more comprehensive validation for XML data than DTD.
• Standardization: XSD is an XML-based standard, making it easier to integrate with
XML technologies.

32
XML Parsing

• XML Parsing refers to the process of reading and processing XML data.
• Parsing transforms XML into a format that can be manipulated programmatically.
• In order to process XML data, every program or server process needs an XML
parser
• An XML parser is a software library or package that provides interfaces for
client applications to work with an XML document
• The XML Parser is designed to read the XML document and create a way(interface or
API) for programs to use XML

33
Cont’d…

There are two types of parsers


͋ DOM Parser
͋ SAX Parser
• SAX (Simple API for XML) and DOM (Document Object Model)
• They allow programmers to access their information stored in XML documents

• Both of them take very different approaches to giving you access to your
information

34
Cont’d…

• DOM (Document Object Model): is a platform that allows programs and scripts
to dynamically access and manipulate the content and structure of a XML documents.
• It is a programming API for access XML documents.
• It can be used with any programming language.
• DOM exposes the whole document to applications.

• It presents an XML document as a tree-structure.

• The tree structure makes easy to describe an XML document.


• Pros and Cons: While DOM provides easy access to elements, it can consume
significant memory for large XML files.
35
Cont’d…

• SAX (Simple API for XML): It is a streaming parser that reads the XML document
sequentially and triggers events as it encounters different elements.
• It is not as a tree of nodes, but as a sequence of events
• Usage: Efficient for large documents as it uses less memory by not loading the entire
document.
• If you need not to modify the document you can use SAX
• Pros and Cons: SAX is faster and requires less memory, but it does not allow for
random access to data.

36
Other XML Binding APIs
• XML Binding APIs facilitate the conversion between XML documents and
programming language objects.
• They automate the process of reading XML data into objects and writing objects back
into XML format.
JAXB (Java Architecture for XML Binding):
• JAXB simplifies the binding of XML schemas to Java representations, allowing
developers to easily marshal (convert Java objects to XML) and unmarshal (convert
XML to Java objects).
• Features: Supports schema generation, validation, and custom annotations to control
the binding process. 37
Cont’d…

Example of JAXB Annotations:


@XmlRootElement
public class Book {
@XmlElement
private String title;
@XmlElement
private String author;
@XmlElement
private BigDecimal price;
}

38
Cont’d…
Apache XMLBeans:
• A library for binding XML schema to Java types, allowing for easy manipulation of
XML data in a type-safe manner.
• Features: Supports both XML validation and XML generation from Java objects.
XStream:
• A library that converts Java objects to XML and back, focusing on simplicity and
flexibility.
• Features: Supports complex object graphs and collections without requiring the
definition of an XML schema.
39
Structure of SOAP Messages

• SOAP (Simple Object Access Protocol) is a protocol designed for exchanging


structured information in web services.
• It uses XML to define the format of the messages
• SOAP can be used for broadcasting a message.
• SOAP is platform- and language-independent.
• SOAP is the XML way of defining what information is sent and how.

40
Structure of SOAP Messages

Features of SOAP:
• Extensibility: SOAP allows the inclusion of additional information through headers.
• Neutrality: Operates over various transport protocols (e.g., HTTP, SMTP).
• Independence: Platform and programming language agnostic, allowing diverse
systems to communicate.

41
Cont’d…

Structure of SOAP Messages


A SOAP message consists of the following elements, all enclosed within an Envelope:
• Envelope: The root element that defines the start and end of the message. It has two
main parts:
o Header (Optional): Contains metadata like authentication, security, or transaction
information.
o Body: The main content of the message (data being transferred).

42
Cont’d…

• Fault (Optional): Used to handle errors that occur during message processing. It
provides error details including a fault code and a description if something goes wrong.
Example of SOAP message
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<Authentication>
<Token>12345</Token>
</Authentication>
</soap:Header>
<soap:Body>
<GetBookResponse>
<Title>Learning XML</Title>
<Author>John Doe</Author>
</GetBookResponse>
</soap:Body>
</soap:Envelope> 43
Anatomy of a WSDL Document

• WSDL (Web Services Description Language) is an XML-based language used to


describe the functionality offered by a web service.
• A WSDL document defines the service's operations, input/output messages, and the
protocol used for communication.
Key Elements of WSDL:
• <definitions>: The root element that encapsulates the entire WSDL document.
• <types>: Defines the data types used by the web service operations.
• <message>: Describes the input and output messages exchanged between the client
and the service.
44
Cont’d…

• <portType>: Defines the operations available in the web service, along with their
associated messages.
• <binding>: Specifies the communication protocols (e.g., SOAP) used to interact with
the service.
• <service>: Defines the endpoint(s) for accessing the web service.

45
Cont’d…
<definitions>
<message name="sayHelloRequest">
WSDL Example <part name="name" type="xsd:string"/>
</message>
<message name="sayHelloResponse">
<part name="greeting" type="xsd:string"/>
</message>

<portType name="HelloServicePort">
<operation name="sayHello">
<input message="sayHelloRequest"/>
<output message="sayHelloResponse"/>
</operation>
</portType>

<service name="HelloService">
<port name="HelloServicePort">
<soap:address
location="http://example.com/HelloService"/>
</port>
46
</service>
Cont’d…

Key Elements of the previous example:


• Messages: sayHelloRequest (input) & sayHelloResponse (output).
• PortType: Operation sayHello with request/response.
• Service: Service available at http://example.com/HelloService.

47
Cont’d…
Specifying Implementation:
• Binding: Defines how the service is implemented, detailing the protocol and message
format.
• Endpoint Address: Specifies the location where the web service can be accessed. This
is crucial for clients to connect to the service.
Implementing the Service:
• Based on the WSDL, developers create the actual service implementation that adheres
to the specified operations and messages.
• Tools like Apache CXF or JAX-WS can automate the generation of server-side code from
WSDL, reducing development time and ensuring compliance with the defined service
48 contract.
THE END!!
49

You might also like