Loading TOC...

fn:document

fn:document(
   $uris as item()*,
   [$base-node as node()]
) as document-node()*

Summary

Returns the document(s) stored in the database at the specified URI(s). The URI(s) are resolved according to the base-uri of the calling stylesheet or XQuery main module.

This is an XSLT function, and it is available in both XSLT and in XQuery 1.0-ml.

Parameters
$uris The $uris is a sequence of the URI(s) of the document(s) to be retrieved. This parameter is mandatory. However you may pass a singleton sequence with an empty string in it. In that case it will return the stylesheet that contains this function call when called from XSLT stylesheet and all the documents in the database when called from XQuery- this is allowed only when you are not using version 1.0 strict. If any URI in this sequence is an absolute URI, then it is used as is. If it is a relative URI, it is resolved against a base URI specified in the second argument.
base-node If $base-node is supplied, its base URI is used to resolve relative URIs in uri-sequence. If it is not supplied, the base URI of the node that contained the fn:document() call is used.

Usage Notes

If no second argument is specified, the URI resolves using the base-uri of the calling module. This can cause surprising results if the URI you are resolving is not rooted but the module from which you call it has a base-uri. When calling fn:document from an xdmp:eval, the calling module is defined to have no base-uri. When calling from an XQuery module or an XSLT stylesheet, the base-uri is the URI of the module or stylesheet. For an example to demonstrate this , see the second example below.

For the URI to be exactly what you enter, use fn:doc instead.

Example

fn:document(("/product.xml", "/price.xml"),
            <a xml:base="http://www.marklogic.com" />)

Example

xquery version "1.0-ml";
(:
  This module should be at the App Server root with a name test.xqy,
  so it resolves to /test.xqy from the App Server's root.
:)
xdmp:set-response-content-type("text/plain"),

xdmp:document-insert("testing.xml", <foo/>);
xdmp:document-insert("/testing.xml", <bar/>);

declare option xdmp:output "indent-untyped=yes";

<root>
  <document-from-module>
     <document>{fn:document("testing.xml")}</document>
  </document-from-module>
  <document-from-eval>
     <document>{xdmp:eval('fn:document("testing.xml")')}</document>
  </document-from-eval>
</root>

(:
  From the module, the document cannot find the URI "testing.xml"
  because the base-uri of the module is its URI relative to the
  App Server root.  It therefore finds /testing.xml because that is what
  the URI testing.xml resolves to (testing.xml resolves to /testing.xml when
  the base-uri is /test.xqy).  From an eval, there is no base-uri, so
  it can find an unrooted URI (testing.xml resolves to testing.xml when the
  base-uri is empty).  
:)

=>
<root>
  <document-from-module>
    <document>
      <bar/>
    </document>
  </document-from-module>
  <document-from-eval>
    <document>
      <foo/>
    </document>
  </document-from-eval>
</root>

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.