[2.0] The doc() and doc-available() Functions

XSLT 2.0 provides a new function, doc(), that is very similar to the document() function, but that is simpler in a couple of ways. First of all, the document() function can take a node-set or sequence as its first argument, whereas doc() takes a single string. In our earlier stylesheets, we created a variable containing all of the nodes from all of the documents referenced in our list of purchase orders:

<xsl:variable name="purchase-orders"
  select="document(/report/po/@filename)/purchase-order"/>

This returns a sequence of purchase-order nodes, each of which has the structure defined in the purchase order. We can use the variable $purchase-orders in an <xsl:for-each> or <xsl:for-each-group> element. A stylesheet that contains this markup raises an error if there is more than one <po> element in the source document:

<!-- This doesn't work with our sample document -->
<xsl:variable name="purchase-orders"
  select="doc(/report/po/@filename)/purchase-order"/>

If we want to use the doc() function, there are several things we could do. We could specify a particular <po> element:

<xsl:variable name="purchase-orders"
  select="doc(/report/po[2]/@filename)/purchase-order"/>

A more practical solution would be to put the doc() function into an <xsl:for-each> element:

<xsl:for-each select="/report/po">
  <xsl:variable name="purchase-orders"
    select="doc(@filename)/purchase-order"/>
</xsl:for-each>

We could also change our input document so that it has only a single ...

Get XSLT, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.