The document() Function

Contact Us or call 1-877-932-8228
The document() Function

The document() Function

XSLT has a document() function, which is used for selecting nodes from an external XML document. To illustrate, let's look at the following two XML documents.

Code Sample:

MultipleDocs/Demos/BeatlesDocument.xml
<?xml version="1.0"?>
<?xml-stylesheet href="BeatlesDocument.xsl" type="text/xsl"?>
<beatles>
	<beatle link="http://www.paulmccartney.com">
		<name>
			<firstname>Paul</firstname>
			<lastname>McCartney</lastname>
		</name>
	</beatle>
	<beatle link="http://www.johnlennon.com">
		<name>
			<firstname>John</firstname>
			<lastname>Lennon</lastname>
		</name>
	</beatle>
	<beatle link="http://www.georgeharrison.com">
		<name>
			<firstname>George</firstname>
			<lastname>Harrison</lastname>
		</name>
	</beatle>
	<beatle link="http://www.ringostarr.com">
		<name>
			<firstname>Ringo</firstname>
			<lastname>Starr</lastname>
		</name>
	</beatle>
	<beatle link="http://www.webucator.com" real="no">
		<name>
			<firstname>Nat</firstname>
			<lastname>Dunn</lastname>
		</name>
	</beatle>
</beatles>

Code Sample:

MultipleDocs/Demos/BeatlesSongs.xml
<?xml version="1.0" encoding="UTF-8"?>
<Songs>
	<Artist>
		<Name>Paul McCartney</Name>
		<Songs>
			<Song>Love Me Do</Song>
			<Song>Help!</Song>
		</Songs>
	</Artist>
	<Artist>
		<Name>John Lennon</Name>
		<Songs>
			<Song>Helter Skelter</Song>
			<Song>Hey Jude</Song>
		</Songs>
	</Artist>
	<Artist>
		<Name>George Harrison</Name>
		<Songs>
			<Song>Come Together</Song>
			<Song>While My Guitar Gently Weeps</Song>
		</Songs>
	</Artist>
	<Artist>
		<Name>Ringo Starr</Name>
		<Songs>
			<Song>Octopus's Garden</Song>
		</Songs>
	</Artist>
</Songs>

The first document provides basic information about the Beatles, while the second document lists songs that the different Beatles wrote. Now let's take a look at the XSLT below.

Code Sample:

MultipleDocs/Demos/BeatlesDocument.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:import href="HTML.xsl"/>
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<xsl:call-template name="StartHTML">
			<xsl:with-param name="Title" select="'Beatles'"/>
		</xsl:call-template>
	</xsl:template>
	<xsl:template match="beatle[not(@real='no')]">
		<xsl:variable name="BeatleName">
			<xsl:value-of select="name/firstname"/>
			<xsl:text> </xsl:text>
			<xsl:value-of select="name/lastname"/>
		</xsl:variable>
		<li style="color:blue;"><xsl:apply-templates/>
			<ol>
	<xsl:for-each
	select="document('BeatlesSongs.xml')//Artist[Name=$BeatleName]/Songs/Song">
			<li><xsl:value-of select="."/></li>
	</xsl:for-each>
			</ol>
		</li>
	</xsl:template>
	<xsl:template match="beatle">
		<li style="color:red; text-decoration:line-through">
			<xsl:apply-templates/>
		</li>
	</xsl:template>
	<xsl:template match="name">
		<a href="{parent::beatle/@link}">
			<xsl:value-of select="lastname"/>,
			<xsl:value-of select="firstname"/>
		</a>
	</xsl:template>
</xsl:stylesheet>

The xsl:variable element above is used to hold the name of the Beatle currently being processed. The xsl:for-each element uses the document() function to loop through the Song elements in the BeatleSongs.xml document for each Beatle as that Beatle template is processed. To see how it works, transform MultipleDocs/Demos/BeatlesDocument.xml against MultipleDocs/Demos/BeatlesDocument.xsl

Next