document() Function |
Allows you to process multiple source documents in a single stylesheet. This extremely powerful and flexible function is the subject of Chapter 7, so we'll only include a brief overview of the function here.
Inputs
The document() function most commonly takes a string as its argument; that string is treated as a URI, and the XSLT processor attempts to open that URI and parse it. If the string is empty (the function call is document('')), the document() function parses the stylesheet itself. See Section 7.3 in Chapter 7 for all the details on the parameters to the document() function.
Output
A node-set containing the nodes identified by the input argument. Again, Chapter 7 has all the details, so we won't rehash them here.
Defined in
XSLT section 12.1, Multiple Source Documents.
Example
The following example uses the document() function with an empty string to implement a lookup table. Here is our XML document:
    <?xml version="1.0"?>
    <report>
      <title>Miles Flown in 2001</title>
      <month sequence="01">
        <miles-flown>12379</miles-flown>
        <miles-earned>35215</miles-earned>
      </month>
      <month sequence="02">
        <miles-flown>32857</miles-flown>
        <miles-earned>92731</miles-earned>
      </month>
      <month sequence="03">
        <miles-flown>19920</miles-flown>
        <miles-earned>76725</miles-earned>
      </month>
      <month sequence="04">
        <miles-flown>18903</miles-flown>
        <miles-earned>31781</miles-earned>
      </month>
    </report>
We can use the document() function to convert the sequence attribute of the <month> element into the name of the corresponding month. Here is our stylesheet:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:months="Lookup table for month names">

      <months:name sequence="01">January</months:name>
      <months:name sequence="02">February</months:name>
      <months:name sequence="03">March</months:name>
      <months:name sequence="04">April</months:name>
      <months:name sequence="05">May</months:name>
      <months:name sequence="06">June</months:name>
      <months:name sequence="07">July</months:name>
      <months:name sequence="08">August</months:name>
      <months:name sequence="09">September</months:name>
      <months:name sequence="10">October</months:name>
      <months:name sequence="11">November</months:name>
      <months:name sequence="12">December</months:name>

      <xsl:output method="text"/>

      <xsl:variable name="newline">
    <xsl:text>
    </xsl:text>
      </xsl:variable>

      <xsl:template match="/">
        <xsl:value-of select="$newline"/>
        <xsl:text>A test of the document() function:</xsl:text>

        <xsl:value-of select="$newline"/>
        <xsl:value-of select="$newline"/>
        <xsl:for-each select="/report/month">
          <xsl:text>   </xsl:text>
          <xsl:value-of
            select="document('')/*/months:name[@sequence=current()/@sequence]"/>
          <xsl:text> - </xsl:text>
          <xsl:value-of select="format-number(miles-flown, '##,###')"/>
          <xsl:text> miles flown, </xsl:text>
          <xsl:value-of select="format-number(miles-earned, '##,###')"/>
          <xsl:text> miles earned.</xsl:text>
          <xsl:value-of select="$newline"/>
          <xsl:text>      (Averaged </xsl:text>
          <xsl:value-of
            select="format-number(miles-earned div miles-flown, '##.#')"/>
          <xsl:text> miles earned for each mile flown.)</xsl:text>

          <xsl:value-of select="$newline"/>
          <xsl:value-of select="$newline"/>
        </xsl:for-each>
      </xsl:template>

    </xsl:stylesheet>
Here are the results, with the correct month names included in the output:
    A test of the document() function:

       January - 12,379 miles flown, 35,215 miles earned.
          (Averaged 2.8 miles earned for each mile flown.)

       February - 32,857 miles flown, 92,731 miles earned.
          (Averaged 2.8 miles earned for each mile flown.)

       March - 19,920 miles flown, 76,725 miles earned.
          (Averaged 3.9 miles earned for each mile flown.)

       April - 18,903 miles flown, 31,781 miles earned.
          (Averaged 1.7 miles earned for each mile flown.)