184 Part III Manipulating Messages
sales-related information. Transforming the common format into a variety of
output formats, or fanning-out, makes sense because we can then pass this
information to various systems in a variety of output formats without requiring
an additional transformation. This is where XML—and Extensible Stylesheet
Language Transformations (XSLT) in particular—shine. There is a clear need for
transformation services within any EI process, and XSLT is an excellent tool to
perform those transformations.
This chapter will illustrate a few ways you can transform messages to
make your integration solution work well. (We will discuss additional methods
in Chapter 11, where we cover many of the tools available through Microsoft
BizTalk Server 2004.) We begin this chapter by illustrating the most common
language for navigating an XML document, XPath. We will also demonstrate
some of the tools available to make message manipulation easier, such as
UpdateGrams, XML Diff, and XML Patch. We will conclude this chapter by giving
you some ideas on how to enrich your messages when they lack information
and how to filter your messages when you have more data than you need.
Transforming Message Contents by Using XPath
Because the use of XML has grown rapidly over the past few years, we think it
is safe to say that the primary form of messages in modern systems is XML.
Given that, we need a language to query content from the XML documents
used within these systems. XPath is a language created to find parts of an XML
document and designed to be used with XSLT. In this section, we will use XPath
and XSLT to transform XML messages into HTML documents. We could trans-
form XML messages into text files by using Comma Separated Values or different
types of XML documents as well, but that is outside the scope of this chapter.
In XPath, a query is not automatically performed over all the XML con-
tent but always has a starting point or context node. This can be any node in
the node tree that constitutes the document. From this fixed point, you can
issue queries such as, “Give me all your children.” This kind of query makes
sense only if a starting point is defined. This starting point can be the root
node, of course, which would query the entire document. You can think of
the root node as being similar to the root folder of the file system on your
local computer. All other folders can then be found by opening child folders
along a particular path.
Consider the following XML fragment. It consists of a customer’s account
information in the Microsoft Pet Shop application. This XML fragment is part of
a larger document that contains all accounts that were queried from the Pet
Shop database.
Chapter 8 Message Transformation 185
Account XML Fragment
<Account>
<userid>987</userid>
<email>jsmith@contoso.com</email>
<firstname>John</firstname>
<lastname>Smith</lastname>
<status>Married</status>
<addr1>123 Elm Street</addr1>
<addr2></addr2>
<city>Dallas</city>
<state>Texas</state>
<zip>12345</zip>
<country>USA</country>
<phone>123-456-7890</phone>
<Signon>
<username>jsmith</username>
<password>password</password>
</Signon>
<bannerdata>
<favcategory>Cats</favcategory>
<bannername>
<image format="image/jpeg" url="images/banner_cats.gif"
/>
</bannername>
</bannerdata>
<profile>
<userid>abcde</userid>
<langpref>english</langpref>
<favcategory>Cats</favcategory>
<mylistopt>1</mylistopt>
<banneropt>1</banneropt>
</profile>
</Account>
How do you display these accounts in an organized fashion? XPath allows
you to easily select the nodes you need. You then can embed the XPath com-
mands in the Extensible Stylesheet Language (XSL) style sheet to display the
data in a more readable format.
Although we will not cover all of XPath here, a quick summary of some of
the key commands follows:
Child One of the children of the context node.
Descendants All children and the children’s children.
Parent The direct parent (and only the direct parent) of the context
node (if any).
186 Part III Manipulating Messages
Ancestor All ancestors of the context node. Always includes the
root node (unless the root node is the context node).
Following All nodes in the document that come after the current
node (in document order).
Preceding All nodes in the document that come before the current
node (in document order).
Attributes Contains the attributes of the context node.
Self Only the context node itself.
Descendant-or-self All descendants and the context node itself.
Ancestor-or-self All ancestors and the context node itself.
The goal is to produce the output shown in Figure 8-1.
F08LS01
Figure 8-1 Formatted account information displayed in HTML.
Source XML Structure
Account = [no text value]
userid = 987
email = jsmith@contoso.com
fir
stname = Jeff
lastname = Smith
status = Married
addr1 = 123 Elm Street
addr2 = [no text value]
city = Dallas
state = T
exas
zip = 12345
country = USA
phone = 123-456-7890
Signon = [no text value]
username = jsmith
password = password
bannerdata = [no text value]
favcategory = Cats
banner
name = [no text value]
image = [no text value]
Profile = [no text value]
userid = abcde
langpref = english
favcategory = Cats
mylistopt = 1
banneropt = 1

Get Enterprise Integration Solutions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.