PROMOTE MODULARITY AND REUSABILITY WITH XINCLUDE
When working with XML documents, there are several basic concepts that
you need to master. The ability to include external documents within an
XML document is one of the basic functional elements of XML. We'll look
at a few applications of Xinclude, as well as examples of how to put
this basic concept to use.
MODULAR XML
There are many ways to implement and use inclusions in XML documents.
One of the main reasons for using includes is to promote modularity and
reusability. By building XML documents from smaller "component" XML
documents using includes, you'll be able to reuse more components and
write less code from scratch. You will also begin to see various
patterns in your XML documents that result in modules or components.
XInclude allows you to take advantage of XML modularity and implement
it
effectively in your XML solutions.
INCLUSION EXAMPLE
Using XInclude is essentially a trivial process. The XInclude
specification is comprised of two elements - and
. The tag handles the actual inclusion of
external documents into the current document. The tag
is
a child element of and it handles conditions where the
include doesn't succeed (due to a missing file, network failure, or for
some other reason).
Let's look at applying XInclude to a sample scenario. We'll start with
an example XML document, called PurchaseOrder.xml. This document
contains some basic purchase order data, such as customer information
and the ordered items.
PurchaseOrder.xml:
99829923
ATTN: Mr. Wobbles
Widgetwoo Corp.
1234 W Broadway
New York, NY 10010
ATTN: Accounts
Widgetwoo Corp.
1234 W Broadway
New York, NY 10010
-
50
88344
Super Foowacker
602.33
-
200
9003
Turbo Electrifoo
58.50
Now, we'll split this document into two documents. We'll place all of
the elements into the first document, which
we'll
call CustomerInformation.xml. Then, in the PurchaseOrder.xml document,
we'll include the customer information using XInclude.
CustomerInformation.xml:
99829923
ATTN: Mr. Wobbles
Widgetwoo Corp.
1234 W Broadway
New York, NY 10010
ATTN: Accounts
Widgetwoo Corp.
1234 W Broadway
New York, NY 10010
PurchaseOrder.xml:
-
50
88344
Super Foowacker
602.33
-
200
9003
Turbo Electrifoo
58.50
The result of processing the new PurchaseOrder.xml will be the same as
if the document looked like the one we started out with. The essential
difference is that now we have moved some of the data out of the main
document and used XInclude.
CAVEATS
There are a handful of caveats related to using XInclude. The first is
that your include may fail. To illustrate, what happens when the
document you're including is on a Web server in Germany and the network
goes down? To handle this scenario, the XInclude specification provides
the element. You can use to indicate
alternate behavior when an fails.
Another major caveat is to avoid circular references. Imagine if our
CustomerInformation.xml document contained an include of
PurchaseOrder.xml (which includes CustomerInformation.xml). Because
each
document includes the other, the references become circular and
infinitely nested. A mechanism that uses circular references is not
only
badly designed but also does not fall within the XInclude
specification.
To find out more about XInclude and read the specification, visit the
W3C's Web site.
http://www.w3.org/TR/xinclude/
Brian Schaffner is a senior consultant for Fujitsu Consulting. He
provides architecture, design, and development support for Fujitsu's
Telcom360
group.