[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"> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access