This proposal provides a file system API for XPath 2.0. It defines extension functions to perform file system related operations such as listing, reading, or writing files. It has been designed to be compatible with XQuery 1.0 and XSLT 2.0, as well as any other XPath 2.0 usage.

Introduction

Namespace Conventions

The module defined by this document defines several functions all contained in the namespace http://expath.org/ns/file. In this document, the file prefix, when used, is bound to this namespace URI.

Error codes are defined in the namespace http://expath.org/ns/error. In this document, the err prefix, when used, is bound to this namespace URI.

File Paths vs. URIs

In order to make the file API more accessible, paths referring to directories of files are specified as strings. The syntax of such strings is implementation defined. However, we strongly recommend that the following forms are accepted and interpreted as described below.

In the following, only "paths" is used if referring to a file or directory.

Error management

Error conditions are identified by an error code (a QName). If such an error condition is reached during the execution of the function, a dynamic error is raised, with the corresponding error code (as if the standard XPath function error had been called).

Error codes are defined throughout the spec. For too many reasons to enumerate here, the file operations can raise an error. In this case, if the error condition is not mentioned explicitly in the spec, the implementation must raise an error with an appropriate message [=err:FS001=].

Functions

file:copy

This function copies a file given a source and a destination. The operation fails by returning false if the $source path does not point to a file. If the $overwrite parameter is missing or evaluates to false, the function returns false if the destination already exists. Otherwise, the destination file, if it exists, will be overwritten. The function returns true if the copy operation was successful.

file:exists

Tests if a path is already used in the file system. The function returns true if the file or directory pointed to by the $fileOrDir parameter exists already. Otherwise, the function returns false.

file:files

Lists all files in a given directory. The order of the files in the result is not defined. The special files "." and ".." are never returned. The returned paths are relative to the provided $path. If the optional $pattern parameter is provided, only the file having a name that is matching the given regular expression pattern are returned. An additional $recursive parameter indicates if the search should recurse in the subdirectories.

file:is-directory

Tests if a path/URI points to a directory. The function returns true if the path/URI points to a directory. Otherwise, it returns false. On UNIX-based systems, the root and the volumes roots are considered directories.

file:is-file

Tests if a path points to a regular file. The function returns true if the path points to a regular file. Otherwise, the function returns false.

file:last-modified

Retrieves the timestamp of the last modification of the file system item (e.g. file, directory, or symbolic link) pointed by the given path ($fileOrDir).

file:mkdir

Creates a directory. This function is not recursive. The optional $create parameter indicates that the function should succeed only if the target directory can be created. The function returns true if the operation succeeded. Otherwise, the function returns false.

file:mkdirs

Creates directories recursively. The optional $create parameter indicates that the function should succeed only if the target directory can be created. The function returns true if the operation succeeded. Otherwise, the function returns false.

file:path-separator

This function returns the file path separator used by the operating system. For example, it returns "/" on Unix-based platforms or "\" on Windows platforms.

file:path-to-full-path

Transforms a path into a full operating system path. The resulting URI must have the file:// scheme. The operation is performed regardless the existence of a file or directory referred to by the provided path.

file:path-to-uri

Transforms a file system path into a URI with the file:// scheme. No checks are performed regardless of the existence of a fil or directory referred to by the provided file system path.

file:read

Reads the content of a file pointed to by the $file parameter and returns a Base64 representation of the content.

file:read-html

Read the content of the HTML file pointed to by the $file parameters and returns it as a string.

file:read-text

Reads the content of a file and returns the string representation of the content.

file:read-xml

Reads a file and returns the parsed an XML document. If the $tidy parameter is present and evaluates to true, the implementation might perform a cleaning step (e.g. as performed by the tidy library) in order to make sure that a valid XML document is obtained. If the $tidy parameter is not present or evaluates to false and the file does not contain a well-formed XML document, an error is raised (TODO error code). Cleaning documents before parsing in order to make them well-formed might be useful for HTML documents.

file:remove

Delete a file or a directory from the file system. This operation is not recursive. The function returns false if the operation failed (e.g. if a non-empty directory should be deleted or the file or directory does not exist). Otherwise, the function returns true.

file:write

Write a sequence of items to a file. This operation creates a new file or appends the serialized content to the file pointed by the given path/URI. If the $append flag is true and the file does not exist, a new one is created.

The $serializer-params parameter is used to set the corresponding serialization parameter defined in [[xslt-xquery-serialization]], as defined for the XPath 2.1 function fn:serialize().

The function returns true if the file was written successfully, or false otherwise.

Summary of Error Conditions

err:FS001
A file system error occurred.