The document() Function

A common task in writing stylesheets is combining data from different documents. We’ll start by using the document() function to parse and process multiple XML documents. We’ll start our discussion with XML-tagged purchase orders that look like this:

<?xml version="1.0"?>
<!-- po38295.xml -->
<purchase-order id="38295">
  <date year="2001" month="9" day="8"/>
  <customer id="4738" level="Basic">
    <address type="business">
      <name>
        <title>Ms.</title>
        <first-name>Amanda</first-name>
        <last-name>Reckonwith</last-name>
      </name>
      <street>930-A Chestnut Street</street>
      <city>Lynn</city>
      <state>MA</state>
      <zip>02930</zip>
    </address>
    <address type="ship-to"/>
  </customer>
  <items>
    <item part-no="23813-03-CDK">
      <name>Cucumber Decorating Kit</name>
      <qty>1</qty>
      <price>29.95</price>
    </item>
  </items>
</purchase-order>

If we had a few dozen documents like this, we might want to view the collection of purchase orders in a number of ways. We could view them sorted (or even grouped) by customer, by part number, by the amount of the total order, by the state to which they were shipped, etc. One way to do this would be to write code that worked directly with the Document Object Model. We could parse each document, retrieve its DOM tree, and then use DOM functions to order and group the various DOM trees, display certain parts of the DOM trees, etc. Because this is an XSLT book, though, you probably won’t be surprised to learn that XSLT provides a function to handle most of the heavy lifting ...

Get XSLT, 2nd Edition 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.