Accessing multiple documents with XSLT

It is nothing new for a programmer to have to look up information from multiple sources and combine it for a single output. But how can you do it in XML? Find out by following our example on the use of XSLT.

XML Stylesheet Transformations (XSLT) provides a robust set of functions for transforming one XML document into another. This is particularly useful when incoming or outgoing XML documents don't match the document type expected by the target system. There are many times when the target system might expect a single document even though the source data is being distributed across multiple documents. We can solve this problem by using XSLT to transform multiple-source documents into a single XML document.

A sample problem
We'll start by defining a simple scenario to illustrate the problem. Assume our customers are going to send us their orders using XML documents. We want to protect the price information in the XML documents because different customers have different negotiated prices, which can change often. Rather than put a per-item price in the XML, the customers will enter the price code for the particular item. For simplicity, we'll say that there are five price codes being used based on the letters A through E. Each price code corresponds to the dollar amount listed in our proprietary Price.xml document, which we update regularly. Unfortunately, our target order system doesn't understand Price.xml. Instead, it expects each line item in the order to have a real price value when input into the system.

The order
An example of the order XML document is shown in Listing A. This order contains two line items. The first item has a price code of E and the second has a price code of C.

Our price codes are kept in Price.xml, shown in Listing B.

As the orders come in, we want to translate the <PriceCode> element into a <Price> element. We'll do this using XSLT's document() function and an XPath expression to extract the correct price code.

The document() function and XPath
Using the document() function, you can access multiple XML documents from within your XSLT template. You can open as many documents as you need. The basic process we'll use to open and access external documents is to assign the <PriceCodes> node of the Price.xml document to a variable inside the translation and then use XPath to query that node for the particular price.

To open a document and assign it to a variable, use the following syntax in your XSLT template:
<xsl:variable name="prices" select=

This will open the Price.xml document in the current directory, find the <PriceCodes> node, and assign it to the prices variable. Now we can query this node to extract a price for a particular code using XPath. To translate the price code into the price, you would use something like the following:
<xsl:variable name="letter" select="PriceCode">
<xsl:value-of select=

This extracts the value of the PriceCode from the input document into a variable called $letter. Then, it uses the $letter variable to identify a particular price using an XPath query. Below is a snippet of our XSLT template that will put all of this together.
<xsl:variable name="prices" select=

<xsl:template match="Item">
<xsl:variable name="letter" select="PriceCode">
  <SKU><xsl:value-of select="SKU"></SKU>
  <Quantity><xsl:value-of select=
  <Price><xsl:value-of select=
"$prices/Code[Letter=$letter]/Price"/> </Price>

An easy way to gather your info
Using XSLT templates to translate XML documents can be a very powerful way to move data from one system to another. Sometimes, this can be challenging if the source data is in multiple documents. You can use XSLT's document() function to access the data in multiple XML documents to solve this problem.

Are you using XML?
Have you implemented XML applications successfully? Does XML still have you tied in knots? Tell us the good and the bad. Send us an e-mail with your thoughts and experiences or post a comment below.


Editor's Picks

Free Newsletters, In your Inbox