This document is also available in these non-normative formats: XML and Revision markup.
Copyright © 2010-2013 Christian Grün, published by the EXPath Community Group under the W3C Community Contributor License Agreement (CLA). A human-readable summary is available.
This specification was published by the EXPath Community Group. It is not a W3C Standard nor is it on the W3C Standards Track. Please note that under the W3C Community Contributor License Agreement (CLA) there is a limited opt-out and other conditions apply.s Learn more about W3C Community and Business Groups.
This proposal provides a file system API for XPath. It defines extension functions to perform file system related operations such as listing, reading, or writing files or directories. It has been designed to be compatible with XQuery 1.0 and XSLT 2.0, as well as any other XPath 2.0 usage.
1 Status of this document
2 Introduction
2.1 Namespace conventions
2.2 File Paths
2.3 Query Execution
2.4 Error Management
3 File Properties
3.1 file:exists
3.2 file:is-dir
3.3 file:is-file
3.4 file:last-modified
3.5 file:size
4 Input/Output
4.1 file:append
4.2 file:append-binary
4.3 file:append-text
4.4 file:append-text-lines
4.5 file:copy
4.6 file:create-dir
4.7 file:create-temp-dir
4.8 file:create-temp-file
4.9 file:delete
4.10 file:list
4.11 file:move
4.12 file:read-binary
4.13 file:read-text
4.14 file:read-text-lines
4.15 file:write
4.16 file:write-binary
4.17 file:write-text
4.18 file:write-text-lines
5 Paths
5.1 file:name
5.2 file:parent
5.3 file:children
5.4 file:path-to-native
5.5 file:path-to-uri
5.6 file:resolve-path
6 System Properties
6.1 file:dir-separator
6.2 file:line-separator
6.3 file:path-separator
6.4 file:temp-dir
6.5 file:base-dir
6.6 file:current-dir
This document is in a final draft stage. Comments are welcomed at public-expath@w3.org mailing list (archive).
The module defined by this document defines functions and errors in the
namespace http://expath.org/ns/file
. In this document, the
file
prefix is bound to this namespace URI.
The output
prefix is bound to the namespace
http://www.w3.org/2010/xslt-xquery-serialization
. It is used
to specify serialization parameters.
All file paths are specified as strings, and are resolved against the current working directory. The current working directory is implementation-defined: It can e. g. be the file system directory from which a query processor was started.
An implementation must accept absolute and relative UNIX/Linux and Windows paths as well as absolute file URIs. Some examples:
C:\Test Dir\my file.xml
: An absolute path on Windows
platforms.
/Test Dir/my file.xml
: An absolute path on UNIX-based
platforms.
C:\\\Test Dir//\\my file.xml
: An absolute path on Windows
platforms that tolerates an arbitrary number of slashes and backslashes.
my file.xml
: A relative path, pointing to a file in the current
working directory.
file:///C:/Test%20Dir/my%20file.xml
: An absolute file URI on
Windows platforms.
file:///Test%20Dir/my%20file.xml
: An absolute path on UNIX-based
platforms.
Before further processing, all paths are normalized to an implementation-defined representation (which usually is the representation of the underlying operating system).
An implementation may choose to raise [file:invalid-path] if a path is invalid.
If a function returns a string that refers to a directory, it will always be suffixed with the system-specific directory separator.
The function file:base-dir can be used to resolve file operations against the directory of the base URI:
let $filename := "input.txt" let $dir := file:base-dir() let $path := concat($dir, $filename) return file:read-text($path)
Some function are marked as ·nondeterministic·, which means they are not guaranteed to perform the same operations and produce identical results from repeated calls. A query processor must ensure that these functions are not relocated or pre-evaluated and that its results are not cached when compiling and evaluating the query and serializing its results.
Error conditions are identified by a code (a QName
). When such an error
condition is reached during the execution of the function, a dynamic error is thrown,
with the corresponding error code (as if the standard XPath function
fn:error
had been called).
Error codes are defined through the specification. The generic error [file:io-error] with an appropriate message is raised for I/O faults, or for specific errors caused by the underlying platform or programming language.
For a list of specific errors see the "Summary of Error Conditions" section of this document.
file:exists
file:exists
($path asxs:string
) asxs:boolean
Tests if the file or directory pointed by $path
exists.
This function is ·nondeterministic·.
file:is-dir
file:is-dir
($path asxs:string
) asxs:boolean
Tests if $path
points to a directory. On UNIX-based systems the root and
the volume roots are considered directories.
This function is ·nondeterministic·.
file:is-file
file:is-file
($path asxs:string
) asxs:boolean
Tests if $path
points to a file.
This function is ·nondeterministic·.
file:last-modified
file:last-modified
($path asxs:string
) asxs:dateTime
Returns the last modification time of a file or directory.
This function is ·nondeterministic·.
$path
does not
exist.file:size
file:size
($file asxs:string
) asxs:integer
Returns the byte size of a file, or the value 0
for directories.
This function is ·nondeterministic·.
$path
does not
exist.file:append
file:append
($file asxs:string
, $items asitem()*
) asempty-sequence()
file:append
($file asxs:string
, $items asitem()*
, $params aselement(output:serialization-parameters)
) asempty-sequence()
Appends a sequence of items to a file. If the file pointed by $file
does
not exist, a new file will be created.
$params
controls the way the $items
items are serialized.
The semantics of $params
is the same as for the
fn:serialize
function in [XQuery and XPath Functions and Operators 3.0]. This consists of an
output:serialization-parameters
element whose format is defined in
[XSLT and XQuery Serialization 3.0]. In contrast to fn:serialize,
the encoding stage will not be skipped by this function.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.file:append-binary
file:append-binary
($file asxs:string
, $value asxs:base64Binary
) asempty-sequence()
Appends a Base64 item as binary to a file. If the file pointed by $file
does not exist, a new file will be created.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.file:append-text
file:append-text
($file asxs:string
, $value asxs:string
) asempty-sequence()
file:append-text
($file asxs:string
, $value asxs:string
, $encoding asxs:string
) asempty-sequence()
Appends a string to a file. If the file pointed by $file
does not exist,
a new file will be created.
The optional parameter $encoding
, if not provided, is considered to be
UTF-8
.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.$encoding
is invalid or
not supported by the implementation.file:append-text-lines
file:append-text-lines
($file asxs:string
, $values asxs:string*
) asempty-sequence()
file:append-text-lines
($file asxs:string
, $lines asxs:string*
, $encoding asxs:string
) asempty-sequence()
Appends a sequence of strings to a file, each followed by the system-dependent
newline character. If the file pointed by $file
does not exist, a new
file will be created.
The optional parameter $encoding
, if not provided, is considered to be
UTF-8
.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.$encoding
is invalid or
not supported by the implementation.file:copy
file:copy
($source asxs:string
, $target asxs:string
) asempty-sequence()
Copies a file or a directory given a source and a target path/URI. The following
cases may occur if $source
points to a file:
$target
does not exist, it will be created.$target
is a file, it will be overwritten.$target
is a directory, the file will be created in that
directory with the name of the source file. If a file already exists, it will be
overwritten.The following cases may occur if $source
points to a directory:
$target
does not exist, it will be created as directory, and all
files of the source directory are copied to this directory with their existing
local names.$target
is a directory, the source directory with all its files
will be copied into the target directory. At each level, if a file already exists
in the target with the same name as in the source, it is overwritten. If a directory
already exists in the target with the same name as in the source, it is not removed,
it is recursed in place (if it does not exist, it is created before recursing).Other cases will raise one of the errors listed below.
The function returns the empty sequence if the operation is successful. If an error occurs during the operation, no rollback to the original state will be possible
This function is ·nondeterministic·.
$source
path does not
exist.$source
points to a
directory and $target
points to an existing file.$source
does not exist.$source
points to a file
and $target
points to a directory, in which a subdirectory exists
with the name of the source file.file:create-dir
file:create-dir
($dir asxs:string
) asempty-sequence()
Creates a directory, or does nothing if the directory already exists. The operation will create all non-existing parent directories.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
file:create-temp-dir
file:create-temp-dir
($prefix asxs:string
, $suffix asxs:string
) asxs:string
file:create-temp-dir
($prefix asxs:string
, $suffix asxs:string
, $dir asxs:string
) asxs:string
Creates a temporary directory and all non-existing parent directories and returns the full path to the created directory.
The temporary directory will not be automatically deleted after query execution. It is guaranteed to not already exist when the function is called.
If $dir
is not given, the directory will be created inside
the system-dependent default temporary-file directory.
This function is ·nondeterministic·.
file:create-temp-file
file:create-temp-file
($prefix asxs:string
, $suffix asxs:string
) asxs:string
file:create-temp-file
($prefix asxs:string
, $suffix asxs:string
, $dir asxs:string
) asxs:string
Creates a temporary file and all non-existing parent directories and returns the full path to the created file.
The temporary file will not be automatically deleted after query execution. It is guaranteed to not already exist when the function is called.
If $dir
is not given, the directory will be created inside
the system-dependent default temporary-file directory.
This function is ·nondeterministic·.
file:delete
file:delete
($path asxs:string
) asempty-sequence()
file:delete
($path asxs:string
, $recursive asxs:boolean
) asempty-sequence()
Deletes a file or a directory from the file system.
If the optional parameter $recursive
is set to true()
,
sub-directories will be deleted as well.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$path
does not
exist.$file
points to a
non-empty directory.file:list
file:list
($dir asxs:string
) asxs:string*
file:list
($dir asxs:string
, $recursive asxs:boolean
) asxs:string*
file:list
($dir asxs:string
, $recursive asxs:boolean
, $pattern asxs:string
) asxs:string*
Lists all files and directories in a given directory. The order of the items in the
resulting sequence is not defined. The "." and ".." items are never returned. The
returned paths are relative to the provided directory $dir
.
If the optional parameter $recursive
is set to true()
, all
directories and files will be returned that are found while recursively traversing
the given directory.
The optional $pattern
parameter defines a name pattern in the glob
syntax. If this is provided, only the paths of the files and directories whose names
are matching the pattern will be returned.
An implementation must support at least the following glob syntax for the pattern:
*
for matching any number of unknown characters and?
for matching one unknown character.A related function is file:children.
This function is ·nondeterministic·.
$dir
does not point to an existing directory.file:move
file:move
($source asxs:string
, $target asxs:string
) asempty-sequence()
Moves a file or a directory given a source and a target path/URI. The following cases
may occur if $source
points to a file:
$target
does not exist, it will be created.$target
is a file, it will be overwritten.$target
is a directory, the file will be created in that
directory with the name of the source file. If a file already exists, it will be
overwritten.The following cases may occur if $source
points to a directory:
$target
does not exist, it will be created as directory, and all
files of the source directory are moved to this directory with their existing
local names.$target
is a directory, the source directory with all its files
will be moved into the target directory. If the target directory contains a
directory with the same name as the source, the error [file:is-dir] is raised.Other cases will raise one of the errors listed below.
The function returns the empty sequence if the operation is successful. If an error occurs during the operation, no rollback to the original state will be possible
This function is ·nondeterministic·.
$source
path does not
exist.$source
points to a
directory and $target
points to an existing file.$source
does not exist.$target
points to a directory,
in which a subdirectory exists with the name of the source.file:read-binary
file:read-binary
($file asxs:string
) asxs:base64Binary
file:read-binary
($file asxs:string
, $offset asxs:integer
) asxs:base64Binary
file:read-binary
($file asxs:string
, $offset asxs:integer
, $length asxs:integer
) asxs:base64Binary
Returns the content of a file in its Base64 representation.
The optional parameters $offset
and $length
can be
used to read chunks of a file.
This function is ·nondeterministic·.
$file
does not
exist.$file
points to a
directory.$offset
or
$length
is negative, or if the chosen values would exceed the file bounds.file:read-text
file:read-text
($file asxs:string
) asxs:string
file:read-text
($file asxs:string
, $encoding asxs:string
) asxs:string
Returns the content of a file in its string representation.
The optional parameter $encoding
, if not provided, is considered to be
UTF-8
.
This function is ·nondeterministic·.
$file
does not
exist.$file
points to a
directory.$encoding
is invalid or
not supported by the implementation.file:read-text-lines
file:read-text-lines
($file asxs:string
) asxs:string*
file:read-text-lines
($file asxs:string
, $encoding asxs:string
) asxs:string*
Returns the contents of a file as a sequence of strings, separated at newline boundaries.
The optional parameter $encoding
, if not provided, is considered to be
UTF-8
.
The newline handling is the same as for the fn:unparsed-text-lines
function in [XQuery and XPath Functions and Operators 3.0].
This function is ·nondeterministic·.
$file
does not
exist.$file
points to a
directory.$encoding
is invalid or
not supported by the implementation.file:write
file:write
($file asxs:string
, $items asitem()*
) asempty-sequence()
file:write
($file asxs:string
, $items asitem()*
, $params aselement(output:serialization-parameters)
) asempty-sequence()
Writes a sequence of items to a file.
If $file
already exists, it will be overwritten; otherwise, it will be created.
$params
controls the way the $items
items are serialized.
The semantics of $params
is the same as for the
fn:serialize
function in [XQuery and XPath Functions and Operators 3.0]. This consists of an
output:serialization-parameters
element whose format is defined in
[XSLT and XQuery Serialization 3.0]. In contrast to fn:serialize, the encoding stage will not be
skipped by this function.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.file:write-binary
file:write-binary
($file asxs:string
, $value asxs:base64Binary
) asempty-sequence()
file:write-binary
($file asxs:string
, $value asxs:base64Binary
, $offset asxs:integer
) asempty-sequence()
Writes a Base64 item as binary to a file.
If $file
already exists, it will be overwritten; otherwise, it will be created.
If the optional parameter $offset
is specified, data will be written
to this file position. An existing file may be resized by that operation.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.$offset
is negative,
or if it exceeds the current file size.file:write-text
file:write-text
($file asxs:string
, $value asxs:string
) asempty-sequence()
file:write-text
($file asxs:string
, $value asxs:string
, $encoding asxs:string
) asempty-sequence()
Writes a strings to a file. If $file
already exists, it will be
overwritten.
The optional parameter $encoding
, if not provided, is considered to be
UTF-8
.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.$encoding
is invalid or
not supported by the implementation.file:write-text-lines
file:write-text-lines
($file asxs:string
, $values asxs:string*
) asempty-sequence()
file:write-text-lines
($file asxs:string
, $values asxs:string*
, $encoding asxs:string
) asempty-sequence()
Writes a sequence of strings to a file, each followed by the system-dependent newline character.
If $file
already exists, it will be overwritten; otherwise, it will be created.
The optional parameter $encoding
, if not provided, is considered to be
UTF-8
.
The function returns the empty sequence if the operation is successful.
This function is ·nondeterministic·.
$file
does not exist.$file
points to a
directory.$encoding
is invalid or
not supported by the implementation.None of the functions in this section performs any check regarding the existence of the received or returned paths.
file:name
file:name
($path asxs:string
) asxs:string
Returns the name of a file or directory.
An empty string is returned if the path points to the root directory, or if it contains no directory separators.
This function is ·deterministic· (no path existence check is made).
file:parent
file:parent
($path asxs:string
) asxs:string?
Transforms the given path into an absolute path, as specified by file:resolve-path, and returns the parent directory.
The inverse function is file:children.
An empty sequence is returned if the path points to a root directory.
This function is ·nondeterministic·.
file:children
file:children
($path asxs:string
) asxs:string*
Returns the paths of all files and directories that are located in the given directory. The order of the items in the resulting sequence is not defined. The "." and ".." items are never returned.
The inverse function is file:parent; a related function is file:list.
This function is ·nondeterministic·.
$path
does not point to an existing directory.file:path-to-native
file:path-to-native
($path asxs:string
) asxs:string
Transforms a URI, an absolute path, or relative path to a canonical, system-dependent path representation. A canonical path is both absolute and unique and thus contains no redirections such as references to parent directories or symbolic links.
If the resulting path points to a directory, it will be suffixed with the system-specific directory separator.
This function is ·nondeterministic·.
$path
does not
exist.file:path-to-uri
file:path-to-uri
($path asxs:string
) asxs:anyURI
Transforms a file system path into a URI with the file://
scheme. If the
path is relative, it is first resolved against the current working directory.
This function is ·deterministic· (no path existence check is made).
file:resolve-path
file:resolve-path
($path asxs:string
) asxs:string
Transforms a relative path into an absolute operating system path by resolving it against the current working directory.
If the resulting path points to a directory, it will be suffixed with the system-specific directory separator.
This function is ·nondeterministic·.
file:dir-separator
file:dir-separator
() asxs:string
Returns the value of the operating system-specific directory separator, which usually
is /
on UNIX-based systems and \
on Windows systems.
This function is ·nondeterministic·.
file:line-separator
file:line-separator
() asxs:string
Returns the value of the operating system-specific line separator, which usually is
on UNIX-based systems,
on
Windows systems and
on Mac systems.
This function is ·nondeterministic·.
file:path-separator
file:path-separator
() asxs:string
Returns the value of the operating system-specific path separator, which usually is
:
on UNIX-based systems and ;
on Windows systems.
This function is ·nondeterministic·.
file:temp-dir
file:temp-dir
() asxs:string
Returns the path to the default temporary-file directory of an operating system.
This function is ·nondeterministic·.
file:base-dir
file:base-dir
() asxs:string?
Returns the parent directory of the static base URI.
If the Base URI property is undefined, the empty sequence is returned. -
If a static base URI exists, and if points to a local file path,
this function returns the same result as the expression
file:parent(static-base-uri())
.