ACCESSING MULTIPLE DOCUMENTS WITH XSLT XML Stylesheet Translations (XSLT) provide a robust set of functions for transforming one XML document into another. This is particularly useful when incoming or outgoing XML documents do not match the document type expected by the target system. There are many times when the target system might expect a single document despite the source data 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 that we can use 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 put 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 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 below. This order contains two line items. The first item has a price code of E and the second has a price code of C. 7723123 1234 1 E 7723 2 C Our price codes are kept in Price.xml, which is shown below. A 10.00 B 42.00 C 6.75 D 322.00 E 15.50 As the orders come in, we want to translate the element into a 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 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: This will open the Price.xml document in the current directory, find the 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: 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. SUMMARY 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. ------------------------------------------