- Sams Teach Yourself XML in 21 Days, Third Edition
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- All About Markup Languages
- All About XML
- Looking at XML in a Browser
- Working with XML Data Yourself
- Structuring Your Data
- Creating Well-Formed XML Documents
- Creating Valid XML Documents
- How XML Is Used in the Real World
- Online XML Resources
- Summary
- Q&A
- Workshop
- Day 2. Creating XML Documents
- Choosing an XML Editor
- Using XML Browsers
- Using XML Validators
- Creating XML Documents Piece by Piece
- Creating Prologs
- Creating an XML Declaration
- Creating XML Comments
- Creating Processing Instructions
- Creating Tags and Elements
- Creating CDATA Sections
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 3. Creating Well-Formed XML Documents
- What Makes an XML Document Well-Formed?
- Creating an Example XML Document
- Understanding the Well-Formedness Constraints
- Using XML Namespaces
- Understanding XML Infosets
- Understanding Canonical XML
- Summary
- Q&A
- Workshop
- Day 4. Creating Valid XML Documents: DTDs
- All About DTDs
- Validating a Document by Using a DTD
- Creating Element Content Models
- Commenting a DTD
- Supporting External DTDs
- Handling Namespaces in DTDs
- Summary
- Q&A
- Workshop
- Declaring Attributes in DTDs
- Day 5. Handling Attributes and Entities in DTDs
- Specifying Default Values
- Specifying Attribute Types
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 6. Creating Valid XML Documents: XML Schemas
- Using XML Schema Tools
- Creating XML Schemas
- Dissecting an XML Schema
- The Built-in XML Schema Elements
- Creating Elements and Types
- Specifying a Number of Elements
- Specifying Element Default Values
- Creating Attributes
- Summary
- Q&A
- Workshop
- Day 7. Creating Types in XML Schemas
- Restricting Simple Types by Using XML Schema Facets
- Creating XML Schema Choices
- Using Anonymous Type Definitions
- Declaring Empty Elements
- Declaring Mixed-Content Elements
- Grouping Elements Together
- Grouping Attributes Together
- Declaring all Groups
- Handling Namespaces in Schemas
- Annotating an XML Schema
- Summary
- Q&A
- Workshop
- Part I. In Review
- Well-Formed Documents
- Valid Documents
- Part II: At a Glance
- Day 8. Formatting XML by Using Cascading Style Sheets
- Our Sample XML Document
- Introducing CSS
- Connecting CSS Style Sheets and XML Documents
- Creating Style Sheet Selectors
- Using Inline Styles
- Creating Style Rule Specifications in Style Sheets
- Summary
- Q&A
- Workshop
- Day 9. Formatting XML by Using XSLT
- Introducing XSLT
- Transforming XML by Using XSLT
- Writing XSLT Style Sheets
- Using <xsl:apply-templates>
- Using <xsl:value-of> and <xsl:for-each>
- Matching Nodes by Using the match Attribute
- Working with the select Attribute and XPath
- Using <xsl:copy>
- Using <xsl:if>
- Using <xsl:choose>
- Specifying the Output Document Type
- Summary
- Q&A
- Workshop
- Day 10. Working with XSL Formatting Objects
- Introducing XSL-FO
- Using XSL-FO
- Using XSL Formatting Objects and Properties
- Building an XSL-FO Document
- Handling Inline Formatting
- Formatting Lists
- Formatting Tables
- Summary
- Q&A
- Workshop
- Part II. In Review
- Using CSS
- Using XSLT
- Using XSL-FO
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Why XHTML?
- Writing XHTML Documents
- Validating XHTML Documents
- The Basic XHTML Elements
- Organizing Text
- Formatting Text
- Selecting Fonts: <font>
- Comments: <!-->
- Summary
- Q&A
- Workshop
- Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Introducing SVG
- Creating an SVG Document
- Creating Rectangles
- Adobe's SVG Viewer
- Using CSS Styles
- Creating Circles
- Creating Ellipses
- Creating Lines
- Creating Polylines
- Creating Polygons
- Creating Text
- Creating Gradients
- Creating Paths
- Creating Text Paths
- Creating Groups and Transformations
- Creating Animation
- Creating Links
- Creating Scripts
- Embedding SVG in HTML
- Introducing SMIL
- Summary
- Q&A
- Workshop
- Day 14. Handling XLinks, XPointers, and XForms
- Introducing XLinks
- Beyond Simple XLinks
- Introducing XPointers
- Introducing XBase
- Introducing XForms
- Summary
- Workshop
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Introducing the W3C DOM
- Introducing the DOM Objects
- Working with the XML DOM in JavaScript
- Searching for Elements by Name
- Reading Attribute Values
- Getting All XML Data from a Document
- Validating XML Documents by Using DTDs
- Summary
- Q&A
- Workshop
- Day 16. Using Java and .NET: DOM
- Using Java to Read XML Data
- Finding Elements by Name
- Creating an XML Browser by Using Java
- Navigating Through XML Documents
- Writing XML by Using Java
- Summary
- Q&A
- Workshop
- Day 17. Using Java and .NET: SAX
- An Overview of SAX
- Using SAX
- Using SAX to Find Elements by Name
- Creating an XML Browser by Using Java and SAX
- Navigating Through XML Documents by Using SAX
- Writing XML by Using Java and SAX
- Summary
- Q&A
- Workshop
- Day 18. Working with SOAP and RDF
- Introducing SOAP
- A SOAP Example in .NET
- A SOAP Example in Java
- Introducing RDF
- Summary
- Q&A
- Workshop
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- XML, Databases, and ASP
- Storing Databases as XML
- Using XPath with a Database
- Introducing XQuery
- Summary
- Q&A
- Workshop
- Day 21. Handling XML in .NET
- Creating and Editing an XML Document in .NET
- From XML to Databases and Back
- Reading and Writing XML in .NET Code
- Using XML Controls to Display Formatted XML
- Creating XML Web Services
- Summary
- Q&A
- Workshop
- Part V. In Review
- Appendix A. Quiz Answers
- Quiz Answers for Day 1
- Quiz Answers for Day 2
- Quiz Answers for Day 3
- Quiz Answers for Day 4
- Quiz Answers for Day 5
- Quiz Answers for Day 6
- Quiz Answers for Day 7
- Quiz Answers for Day 8
- Quiz Answers for Day 9
- Quiz Answers for Day 10
- Quiz Answers for Day 11
- Quiz Answers for Day 12
- Quiz Answers for Day 13
- Quiz Answers for Day 14
- Quiz Answers for Day 15
- Quiz Answers for Day 16
- Quiz Answers for Day 17
- Quiz Answers for Day 18
- Quiz Answers for Day 19
- Quiz Answers for Day 20
- Quiz Answers for Day 21
Introducing SOAP
SOAP lets Web applications send data back and forth in messages in a platform-independent, language-independent way. SOAP was designed so that distributed applications could communicate through corporate firewalls, allowing applications to work with objects across all kinds of boundaries. In SOAP we send XML-based messages by using HTTP; because it uses widely available HTTP, SOAP has become very popular. SOAP messages can provide a backbone for distributed applications, using existing technologies to connect the parts of those distributed applications. We can also send attachments by using SOAP messages, as you'll see today.
SOAP is a general-purpose XML application that is helping to standardize the way people handle data on the Internet. There are plenty of SOAP resources on the Internet; here's a starter list, including the W3C's documentation on it:
- http://www.w3.org/TR/SOAP— The W3C SOAP 1.1 documentation
- http://www.w3.org/TR/SOAP-attachments— The W3C "SOAP Messages with Attachments" documentation
- http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-soap.html— Part 1 of a SOAP tutorial
- http://www.javaworld.com/javaworld/jw-04-2001/jw-0427-soap.html— Part 2 of a SOAP tutorial
- http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-soap.html— Part 3 of a SOAP tutorial
- http://www.javaworld.com/javaworld/jw-07-2001/jw-0706-soap.html— Part 4 of a SOAP tutorial
- http://www.w3schools.com/SOAP/default.asp— A SOAP tutorial
- http://www.xml.com/pub/a/2000/02/09/feature/index.html— An article on SOAP from XML.com
- http://xml.apache.org/soap/faq/index.html— The Apache project's SOAP FAQ
We'll start digging into SOAP now, beginning with the syntax that makes SOAP work.
Understanding SOAP Syntax
A SOAP message is simply an XML document that uses the SOAP syntax rules. There are three parts of a valid SOAP message:
- Envelope— The envelope contains the message itself.
- Header— This optional part contains information about and descriptions of the message.
- Body— The body contains the actual SOAP message.
As a simple example, here's a SOAP message, complete with an envelope and a body. This message indicates the number of laptops in stock at a warehouse (note that soap-env is the usual envelope namespace prefix):
<soap-env:Envelope xmlns:soap-env="http://www.w3.org/2001/12/soap-envelope" soap:soap-enc="http://www.w3.org/2001/12/soap-encoding"> <soap-env:Header> <m:Name xmlns:m="http://www.XMLPowerCorp.com"> XMLPowerCorp </m:Name> </soap-env:Header> <soap-env:Body> <m:numberAvailable xmlns:m="http://www.XMLPowerCorp.com"> <m:laptops>216</m:laptops> </m:numberAvailable> </soap-env:Body> </soap-env:Envelope>
The default namespace setting for a SOAP envelope is "http://schemas.xmlsoap.org/soap/envelope/". The default namespace setting for the document encoding and data types is " http://schemas.xmlsoap.org/soap/encoding/ ".
So what elements are already defined in SOAP?
Introducing the SOAP Elements
The root element in a SOAP message is the <Envelope> element. There are three possible child elements—<Header>, <Body>, and <Fault>. These elements have to use the names Header, Body, and Fault, respectively, although they can have child elements with any names. For example, here's what a SOAP envelope might look like:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" soap-env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> . . . </soap-env:Envelope>
The next element, the <Header> element, is optional. It holds information about the SOAP message, as in the following example, where we're setting the language to U.S. English (<m:locale> and <m:language> are elements we've defined):
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" soap-env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap-env:Header> <m:locale xmlns:m="http://www.XMLPowerCorp.com"> <m:language>en-us</m:language> </m:locale> </soap-env:Header> . . . </soap-env:Envelope>
Next, the <Body> element holds the actual SOAP message:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" soap-env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap-env:Header> <m:locale xmlns:m="http://www.XMLPowerCorp.com"> <m:language>en-us</m:language> </m:locale> </soap-env:Header> <soap-env:Body> <m:numberAvailable xmlns:m="http://www.XMLPowerCorp.com"> <m:laptops>216</m:laptops> </m:numberAvailable> </soap-env:Body> </soap-env:Envelope>
Note that the <Body> element may also contain a <Fault> element, which can hold any errors that occurred:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" soap-env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <soap-env:Header> <m:locale xmlns:m="http://www.XMLPowerCorp.com"> <m:language>en-us</m:language> </m:locale> </soap-env:Header> <soap-env:Body> <m:numberAvailable xmlns:m="http://www.XMLPowerCorp.com"> <m:laptops>216</m:laptops> </m:numberAvailable> <soap-env:Fault> <faultcode>1166</faultcode> <faultstring>Batteries are low</faultstring> </soap-env:Fault> </soap-env:Body> </soap-env:Envelope>
<Fault> elements can have these subelements:
- <faultcode> — Contains an error code.
- <faultstring> — Contains an error string.
- <faultactor> — Specifies the error's source.
- <detail> — Contains the details of the error.
A number of fault codes are already defined for use in the <faultcode> element:
- VersionMismatch — Means that the namespace for the SOAP <Envelope> element was not correct.
- MustUnderstand — Means that an element's content must be understood if this attribute is set to "1".
- Client — Means there was a problem with the message as sent from the client.
- Server — Means that there was a problem with the server.
In addition, SOAP elements can support various attributes, which we'll look at now.
Introducing the SOAP Attributes
The possible attributes we can use in SOAP elements are actor, encodingStyle, and mustUnderstand. For example, the actor attribute lists a URI corresponding to the group that is using the SOAP message, like this:
<soap-env:Header> <m:data xmlns:m="http://www.XMLPowerCorp.com" soap-env:actor="http://www.XMLPowerCorp.com/philosophy" /> <m:language>en-us</m:language> </m:data> </soap-env:Header>
The encodingStyle attribute indicates the data types used in the message. The W3C provides a default schema for SOAP types, which you can find at http://schemas.xmlsoap.org/soap/encoding/:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" soap-env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> . . . </soap-env:Envelope>
The mustUnderstand attribute is used in the <Header> element, and it indicates whether the software interpreting a message must understand a header element or cause a fatal error if it does not. You can set it to a Boolean value, "1" for true or "0" for false:
<soap-env:Header> <m:data xmlns:m="http://www.XMLPowerCorp.com" soap-env:mustUnderstand="0" /> <m:language>en-us</m:language> </m:data> </soap-env:Header>
Now it's time to see SOAP at work.