W3C

File Module

EXPath Candidate Module 14 June 2012

This version:
http://expath.org/spec/file/20120614
Latest version:
http://expath.org/spec/file
Previous version:
http://expath.org/spec/file/20100517
Editors:
Christian Grün, BaseX GmbH
Matthias Brantner, 28msec GmbH
Contributor:
Gabriel Petrovay, 28msec GmbH

This document is also available in these non-normative formats: XML and Revision markup.


Abstract

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.

Table of Contents

1 Introduction
    1.1 Namespace conventions
    1.2 File Paths vs. URIs
    1.3 Query Execution
    1.4 Error Management
2 File Properties
    2.1 file:exists
    2.2 file:is-dir
    2.3 file:is-file
    2.4 file:last-modified
    2.5 file:size
3 Input/Output
    3.1 file:append
    3.2 file:append-text
    3.3 file:append-text-lines
    3.4 file:append-binary
    3.5 file:copy
    3.6 file:create-dir
    3.7 file:delete
    3.8 file:list
    3.9 file:move
    3.10 file:read-binary
    3.11 file:read-text
    3.12 file:read-text-lines
    3.13 file:write
    3.14 file:write-text
    3.15 file:write-text-lines
    3.16 file:write-binary
4 Paths
    4.1 file:base-name
    4.2 file:dir-name
    4.3 file:path-to-native
    4.4 file:path-to-uri
    4.5 file:resolve-path
5 System Properties
    5.1 file:dir-separator
    5.2 file:path-separator
    5.3 file:line-separator

Appendices

A References
B Summary of Error Conditions


1 Introduction

1.1 Namespace conventions

The module defined by this document defines the functions and variables 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.

1.2 File Paths vs. URIs

All file paths are specified as strings, and are resolved against the current working directory. 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 must first be normalized to an implementation-defined representation (which usually is the representation of the underlying operating system).

1.3 Query Execution

Functions on File Properties (Section 2) and Input/Output (Section 3) are ·nondeterministic·, which means they are not guaranteed to perform the same actions and produce identical results from repeated calls. As such, a 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.

1.4 Error Management

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 error had been called).

Error codes are defined through the specification. If file operations raise additional, errors, which may be specific to the underlying platform or programming language, the generic error [err:FILE9999] with an appropriate message is raised.

For a list of specific errors see the "Summary of Error Conditions" section of this document.

2 File Properties

2.1 file:exists

file:exists($path as xs:string) as xs:boolean

Tests if a path/URI is already used in the file system.

The function returns true() if a file or a directory exists at the location pointed by $path.

2.2 file:is-dir

file:is-dir($path as xs:string) as xs:boolean

Tests if $path points to a directory. On UNIX-based systems the root and the volume roots are considered directories.

2.3 file:is-file

file:is-file($path as xs:string) as xs:boolean

Tests if $path points to a file.

2.4 file:last-modified

file:last-modified($path as xs:string) as xs:dateTime

Returns xs:dateTime representing the last modification time.

[err:FILE0001] is raised if $path does not exist.
[err:FILE9999] is raised if any other error occurs.

2.5 file:size

file:size($file as xs:string) as xs:integer

Returns the byte size of a file as integer.

[err:FILE0001] is raised if $path does not exist.
[err:FILE0004] is raised if $path points to a directory.
[err:FILE9999] is raised if any other error occurs.

3 Input/Output

3.1 file:append

file:append($file as xs:string,
            $items as item()*) as empty-sequence()
file:append($file as xs:string,
            $items as item()*,
            $params as element(output:serialization-parameters)) as empty-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.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE9999] is raised if any other error occurs.

3.2 file:append-text

file:append-text($file as xs:string,
                 $value as xs:string) as empty-sequence()
file:append-text($file as xs:string,
                 $value as xs:string,
                 $encoding as xs:string) as empty-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.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE0005] is raised if $encoding is invalid or not supported by the implementation.
[err:FILE9999] is raised if any other error occurs.

3.3 file:append-text-lines

file:append-text-lines($file as xs:string,
                       $values as xs:string*) as empty-sequence()
file:append-text-lines($file as xs:string,
                       $lines as xs:string*,
                       $encoding as xs:string) as empty-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.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE0005] is raised if $encoding is invalid or not supported by the implementation.
[err:FILE9999] is raised if any other error occurs.

3.4 file:append-binary

file:append-binary($file as xs:string,
                   $value as xs:base64Binary) as empty-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.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE9999] is raised if any other error occurs.

3.5 file:copy

file:copy($source as xs:string,
          $target as xs:string) as empty-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:

  1. if $target does not exist, it will be created.
  2. if $target is a file, it will be overwritten.
  3. if $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:

  1. if $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.
  2. if $target is a directory, all files are copied from the source into the target directory. If a file already exists, it will be overwritten.

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

[err:FILE0001] is raised if the $source path does not exist.
[err:FILE0002] is raised if $source points to a directory and $target points to an existing file.
[err:FILE0003] is raised if the parent directory of $source does not exist.
[err:FILE0004] is raised if $source points to a file and $target points to a directory, in which a subdirectory exists with the name of the source file.
[err:FILE9999] is raised if any other error occurs.

3.6 file:create-dir

file:create-dir($dir as xs:string) as empty-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.

[err:FILE0002] is raised if the specified path, or any of its parent directories, points to an existing file.
[err:FILE9999] is raised if any other error occurs.

3.7 file:delete

file:delete($path as xs:string) as empty-sequence()
file:delete($path as xs:string,
            $recursive as xs:boolean) as empty-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.

[err:FILE0001] is raised if $path does not exist.
[err:FILE0004] is raised if $file points to a non-empty directory.
[err:FILE9999] is raised if any other error occurs.

3.8 file:list

file:list($dir as xs:string) as xs:string*
file:list($dir as xs:string,
          $recursive as xs:boolean) as xs:string*
file:list($dir as xs:string,
          $recursive as xs:boolean,
          $pattern as xs:string) as xs: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.

[err:FILE0003] is raised $dir does not point to an existing directory.
[err:FILE9999] is raised if any other error occurs.

3.9 file:move

file:move($source as xs:string,
          $target as xs:string) as empty-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:

  1. if $target does not exist, it will be created.
  2. if $target is a file, it will be overwritten.
  3. if $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:

  1. if $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.
  2. if $target is a directory, all files are moved from the source into the target directory. If a file already exists, it will be overwritten.

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

[err:FILE0001] is raised if the $source path does not exist.
[err:FILE0002] is raised if $source points to a directory and $target points to an existing file.
[err:FILE0003] is raised if the parent directory of $source does not exist.
[err:FILE0004] is raised if $source points to a file and $target points to a directory, in which a subdirectory exists with the name of the source file.
[err:FILE9999] is raised if any other error occurs.

3.10 file:read-binary

file:read-binary($file as xs:string) as xs:base64Binary

Returns the content of a file in its Base64 representation.

[err:FILE0001] is raised if $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE9999] is raised if any other error occurs.

3.11 file:read-text

file:read-text($file as xs:string) as xs:string
file:read-text($file as xs:string,
               $encoding as xs:string) as xs:string

Returns the content of a file in its string representation.

The optional parameter $encoding, if not provided, is considered to be UTF-8.

[err:FILE0001] is raised if $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE0005] is raised if $encoding is invalid or not supported by the implementation.
[err:FILE9999] is raised if any other error occurs.

3.12 file:read-text-lines

file:read-text-lines($file as xs:string) as xs:string*
file:read-text-lines($file as xs:string,
                     $encoding as xs:string) as xs: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].

[err:FILE0001] is raised if $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE0005] is raised if $encoding is invalid or not supported by the implementation.
[err:FILE9999] is raised if any other error occurs.

3.13 file:write

file:write($file as xs:string,
           $items as item()*) as empty-sequence()
file:write($file as xs:string,
           $items as item()*,
           $params as element(output:serialization-parameters)) as empty-sequence()

Writes a sequence of items to a file.

If $file already exists, it will be overwritten.

$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.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE9999] is raised if any other error occurs.

3.14 file:write-text

file:write-text($file as xs:string,
                $value as xs:string) as empty-sequence()
file:write-text($file as xs:string,
                $value as xs:string,
                $encoding as xs:string) as empty-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.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE0005] is raised if $encoding is invalid or not supported by the implementation.
[err:FILE9999] is raised if any other error occurs.

3.15 file:write-text-lines

file:write-text-lines($file as xs:string,
                      $values as xs:string*) as empty-sequence()
file:write-text-lines($file as xs:string,
                      $values as xs:string*,
                      $encoding as xs:string) as empty-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.

The optional parameter $encoding, if not provided, is considered to be UTF-8.

The function returns the empty sequence if the operation is successful.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE0005] is raised if $encoding is invalid or not supported by the implementation.
[err:FILE9999] is raised if any other error occurs.

3.16 file:write-binary

file:write-binary($file as xs:string,
                  $value as base64Binary) as empty-sequence()

Writes a Base64 items as binary to a file. If $file already exists, it will be overwritten.

The function returns the empty sequence if the operation is successful.

[err:FILE0003] is raised if the parent directory of $file does not exist.
[err:FILE0004] is raised if $file points to a directory.
[err:FILE9999] is raised if any other error occurs.

4 Paths

None of the functions in this section performs any check regarding the existence of the received or returned paths.

4.1 file:base-name

file:base-name($path as xs:string) as xs:string
file:base-name($path as xs:string,
               $suffix as xs:string) as xs:string

Returns the last component from $path, deleting any trailing directory separators. If $path consists entirely of directory separator, the empty string is returned. If $path is the empty string, the string "." is returned, signifying the current working directory.

If $suffix is present, it will be trimmed from the end of the result. This can be used to eliminate file extensions.

No path existence check is made.

4.2 file:dir-name

file:dir-name($path as xs:string) as xs:string

This function returns a string denoting the parent directory of $path. Any trailing directory separators are not counted as part of the directory name. If the specified string is empty or contains no directory separators, "." is returned, signifying the current directory.

No path existence check is made.

4.3 file:path-to-native

file:path-to-native($path as xs:string) as xs: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.

No path existence check is made.

[err:FILE9999] is raised if an error occurs while trying to obtain the native path.

4.4 file:path-to-uri

file:path-to-uri($path as xs:string) as xs: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.

No path existence check is made.

4.5 file:resolve-path

file:resolve-path($path as xs:string) as xs:string

Transforms a relative path into an absolute operating system path by resolving it against the current working directory.

No path existence check is made.

5 System Properties

5.1 file:dir-separator

file:dir-separator() as xs:string

Returns the value of the operating system specific directory separator, which usually is / on UNIX-based systems and \ on Windows systems.

5.2 file:path-separator

file:path-separator() as xs:string

Returns the value of the operating system specific path separator, which usually is : on UNIX-based systems and ; on Windows systems.

5.3 file:line-separator

file:line-separator() as xs: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.

A References

XSLT and XQuery Serialization 3.0
XSLT and XQuery Serialization 3.0. Henry Zongaro. W3C Working Draft 14 December 2010.
XQuery and XPath Functions and Operators 3.0
XPath and XQuery Functions and Operators 3.0. Michael Kay. W3C Working Draft 14 December 2010.

B Summary of Error Conditions

err:FILE0001
The specified path does not exist.
err:FILE0002
The specified path already exists.
err:FILE0003
The specified path does not point to a directory.
err:FILE0004
The specified path points to a directory.
err:FILE0005
The specified encoding is not supported.
err:FILE9999
A generic file system error occurred.