RE: [xsl] Select a node in another xml-document

Subject: RE: [xsl] Select a node in another xml-document
From: "Vladimir Stanciu" <vlads@xxxxxx>
Date: Mon, 27 May 2002 13:30:07 +0300

-----Original Message-----
From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx
[mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of
Sent: Monday, May 27, 2002 11:59 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Select a node in another xml-document


Microsoft XML Core Services (MSXML) 4.0 - 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:

   <ancillary_doc href="ancillary.xml"/>
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)

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

In your HTML or text editor, open a new, empty file.
Add the following code to the file:
<?xml version='1.0'?>
   <watermark>Scootney Publishing</watermark>
   <copyright>Copyright 2000, Scootney Publishing. All Rights
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

In your HTML or text editor, open Transform.xsl and locate the <body> line.
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.

Directly below the line that you just added, add the following code:
<DIV style="position:absolute;font-size:96;font-family:Times New Roman;
   <xsl:value-of select="$ancillary//watermark"/>
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
Immediately above the </body> tag, add the following code:
<DIV style="font-size:9">
   <xsl:value-of select="$ancillary//copyright"/>
These lines insert and format the copyright notice string from

Save Transform.xsl.
To view the source and ancillary files

In Internet Explorer, press F5 to refresh the display of Sales.xml.

> This is part of MS documentation for XSLT 1.0. Check it out. The entire
>in .chm format (Windows Help)is available with the MSXML 4.0 Parser free
somewhere at


Vladimir Stanciu - vlads@xxxxxx

 XSL-List info and archive:

Current Thread