Ch-2 -- Defining SOAP Messages with WSDL (1)
Ch-2 -- Defining SOAP Messages with WSDL (1)
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
• 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)
• <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>
• To avoid this error, replace the "<" character with an entity reference:
<message>salary < 1000</message>
There are 5 pre-defined entity references in XML:
• XML does not truncate multiple white-spaces (HTML truncates multiple white-
spaces to one single white-space):
XML Naming Rules
• Element names cannot start with the letters xml (or XML, or Xml, etc)
• Element names can contain letters, digits, hyphens, underscores, and periods
• 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".
• 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
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
External DTD
• Syntax
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…
• 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.
• 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…
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
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…
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
• <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…
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