
fn:document( $uris as item()*, [$base-node as node()] ) as document-node()*
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.
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.
fn:document(("/product.xml", "/price.xml"),
<a xml:base="http://www.marklogic.com" />)
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: Get the most useful answers to questions from the MarkLogic community, or ask your own question.