[2.0] The collection() Function
The collection()
function takes a string as its argument and returns a collection of
nodes. Defined as part of the XPath 2.0 spec, it gives us the ability to
use a URI to retrieve a collection of documents. How those documents are
stored (or whether they’re really documents at all) is
implementation-dependent. In particular, the spec mentions accessing
data in a relational database as a possible implementation of the
collection()
function. The fact that the string passed to the function can be
generated and can contain parameters makes collection()
very flexible.
We’ll look at a short example here. Here’s the document we’ll pass
to the collection()
function:
<?xml version="1.0"?>
<!-- polist.xml -->
<collection>
<doc href="po38292.xml"/>
<doc href="po38293.xml"/>
<doc href="po38294.xml"/>
<doc href="po38295.xml"/>
</collection>
This is very similar to the list of purchase orders we worked with
earlier in this chapter. The stylesheet that invokes the collection()
function looks like
this:
<?xml version="1.0"?> <!-- collection.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>
A test of the collection() function:</xsl:text> <xsl:variable name="docPile" as="node()*" select="collection('polist.xml')"/> <xsl:text>

 The customers in the </xsl:text> <xsl:text>collection are: 
 </xsl:text> <xsl:for-each select="$docPile/purchase-order/customer"> ...
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.