home contribute faq download

FunctX XQuery Functions


Opens a document based on a URI



The fn:doc function returns the document node of the resource found at the specified URI.

If you are accessing documents on a file system, your implementation may require you to precede the file name with file:///, use forward slashes to separate directory names, and escape each space in the file name with %20.

Relative URI references are also allowed. If $uri is a relative URI, it is resolved based on the base URI of the static context. The base URI of the static context may be set by the processor outside the scope of the query, or it may be declared in the query prolog.

Note that the fn:doc function returns the document node, not the root element node. Therefore, you need to include the root element node in your path (catalog in the previous example).

Processors interpret the URI passed to the fn:doc function in different ways. Some, like Saxon, will dereference the URI, that is, go out to the URL and retrieve the resource at that location. Other implementations, such as those embedded in XML databases, consider the URIs to be just names. The processor might take the name and look it up in an internal catalog to find the document associated with that name.

Implementations also have a leeway in how they handle errors when retrieving documents, how they handle different MIME types, and whether or not they validate the documents against a schema or DTD.

The fn:doc function is stable, meaning that it returns the same results each time it is called within a query. If you call the fn:doc function more than once with the exact same argument, within the same query, the result is the same, even if somehow the resource at $uri has changed. Furthermore, the document nodes retrieved from each of these calls are identical to each other.

The fn:doc function should not be confused with the XSLT fn:document function, which is not available in XQuery. The fn:document function has different behavior, in that it will accept multiple URIs, allows the specification of a base URI, and has specific processing defined for handling fragment identifiers.

This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book.

Arguments and Return Type

$uri xs:string? the URI of the document
return value document-node()?


XQuery ExampleResults
the document node of the document at http://www.functx.com/input/order.xml.

See Also

fn:doc-availableWhether a document is available
fn:collectionOpens a collection
fn:document-uriThe URI of a document
functx:open-ref-documentResolves a relative URI and references it, returning an XML document


Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XQuery Services

Recommended Reading: