Process Multiple XML Documents

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Process Multiple XML Documents

You can incorporate XML from external documents by using the document() function. This function takes as an argument either the relative or absolute URL of the document you want to retrieve. The URL is specified as a string or as a node in which the text value of that node resolves to a URL. For example, the following statement loads the Ancillary.xml document and holds it in the ancillary variable.

<xsl:variable name="ancillary" select="document('ancillary.xml')"/>

The Ancillary.xml document is located in the same directory as the original source XML document, and returns the resulting node set in the variable named ancillary. Alternatively, you could declare this ancillary document in the Sales.xml file:

...
<sales>
   <ancillary_doc href="ancillary.xml"/>
...
</sales>

In this case, you would reference it in Transform.xsl as follows:

<xsl:variable name="ancillary" select="document(//ancillary_doc/@href)"/>

In the first case, the argument of the document function is a string. In the second case, the argument is a node set.

With document(), you load the entire contents of the document, and then select elements from the document by using XML Path Language (XPath) expressions.

In this exercise, first you will create the external document, Ancillary.xml. This document contains a watermark and a copyright notice for the sales report. Then you will add code to Transform.xsl to retrieve the Ancillary.xml file and format the data in that file.

To create Ancillary.xml

  1. In your HTML or text editor, open a new, empty file.
  2. Add the following code to the file:
    <?xml version='1.0'?>
    <document>
       <watermark>Scootney Publishing</watermark>
       <copyright>Copyright 2000, Scootney Publishing. All Rights Reserved.</copyright>
    </document>
  3. In the same folder as Sales.xml, save the file as Ancillary.xml. Then close the file.

To update Transform.xsl to extract data from Ancillary.xml

  1. In your HTML or text editor, open Transform.xsl and locate the <body> line.
  2. Immediately below the <body> line, add the following code:
    <xsl:variable name="ancillary" select="document('ancillary.xml')"/>

    This loads the Ancillary.xml document and returns the resulting node set in the variable named ancillary.

  3. Directly below the line that you just added, add the following code:
    <DIV style="position:absolute;font-size:96;font-family:Times New Roman;
          color:#F0F0F0;z-index:-1">
       <xsl:value-of select="$ancillary//watermark"/>
    </DIV>

    This code reads the <watermark> element from Ancillary.xml and formats the string it contains as a watermark. The XPath expression, $ancillary//watermark, resolves to all <watermark> elements in the Ancillary.xml file.

    Note   As an alternative to the variable declaration, you can accomplish the same effect by referencing the external XML document directly, as follows:
    <xsl:value-of select="document('ancillary.xml')//watermark"/>
    However, using a variable to hold the external document is more efficient if you need to reference the document more than once, as is the case in this exercise.

  4. Immediately above the </body> tag, add the following code:
    <DIV style="font-size:9">
       <xsl:value-of select="$ancillary//copyright"/>
    </DIV>

    These lines insert and format the copyright notice string from Ancillary.xml.

  5. Save Transform.xsl.

To view the source and ancillary files

  • In Internet Explorer, press F5 to refresh the display of Sales.xml. The page now looks like this: