<!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><spec xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax" id="spec-top" w3c-doctype="rec" status="ext-review"><header><title>EXPath File Module 4.0</title><version/><w3c-designation>REC-file-40</w3c-designation><w3c-doctype>W3C Editor's Draft</w3c-doctype><pubdate><day>01</day><month>January</month><year>2000</year></pubdate><publoc>
      <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://qt4cg.org/specifications/EXPath/file-40/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://qt4cg.org/specifications/EXPath/file-40/</loc>
    </publoc><altlocs><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="file-40.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Specification in XML format</loc><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="function-catalog.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML function catalog</loc></altlocs><latestloc doc="EXPath File Module 4.0">
      <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://qt4cg.org/specifications/EXPath/file-40/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://qt4cg.org/specifications/EXPath/file-40/</loc>
    </latestloc><prevrec doc="EXPath File Module">
      <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://expath.org/spec/file" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://expath.org/spec/file</loc>
      
    </prevrec><authlist><author role="editor"><name>Christian Grün</name><affiliation>BaseX GmbH</affiliation></author></authlist><errataloc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://www.w3.org/XML/2017/qt-errata/xpath-functions-31-errata.html" xlink:type="simple"/><translationloc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://www.w3.org/2003/03/Translations/byTechnology?technology=xpath-functions-40" xlink:type="simple"/><abstract><p>This document defines a file system API for XPath 4.0. It defines extension functions
        to perform file system related operations such as listing, reading, or writing files
        or directories.</p><p>The document is an update of the original <bibref ref="expath-file"/> specification,
        developed by the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/community/expath/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">EXPath Community
        Group</loc>, defined for <bibref ref="xpath20"/> and published in 2013.</p><p>The principal semantic alteration is use of functional argument defaults available in
        XPath 4.0.</p><p>These functions are defined for use in <bibref ref="xpath-40"/> and
        <bibref ref="xquery-40"/> and <bibref ref="xslt-40"/> and other related XML standards.
        The signatures and summaries of functions defined in this document are available at:
        <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://qt4cg.org/specifications/EXPath/file-40/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://qt4cg.org/specifications/EXPath/file-40/</loc>.</p><p>A summary of changes since published version 1.0 (the XPath 2.0 version) is provided
        at <specref ref="changelog"/>.</p></abstract><status><p>This version of the specification is work in progress. It is produced by the QT4
        Working Group, officially the W3C XSLT 4.0 Extensions Community Group. Individual
        functions specified in the document may be at different stages of review, reflected in
        their <term>History</term> notes. Comments are invited, in the form of GitHub issues at
        <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://github.com/qt4cg/qtspecs" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://github.com/qt4cg/qtspecs</loc>.</p><note role="dedication" id="dedication"><p>The publications of this community group 
        <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="../xquery-40/xpath-40.html#dedication" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">are dedicated</loc> to our co-chair,
        Michael Sperberg-McQueen (1954–2024).</p></note></status><langusage><language id="EN">English</language></langusage><revisiondesc><p/></revisiondesc></header><body><div1 id="intro"><head>Introduction</head><changes><change>If a section of this specification has been updated since version 1.0,
            an overview of the changes is provided, along with links to
            navigate to the next or previous change.</change><change>Sections with significant changes are marked with a ✭ symbol in the
            table of contents. New functions are indicated by ✚.</change></changes><p>The purpose of this document is to define functions to manipulate files within a
        local filesystem and their representations,
        for inclusion in XPath 4.0, XQuery 4.0, and XSLT 4.0.</p><p>The exact syntax used to call these functions and operators is specified in <bibref ref="xpath-40"/>, <bibref ref="xquery-40"/> and <bibref ref="xslt-40"/>. </p><p>This document defines several classes of functions:</p><ulist><item><p>Functions to examine the properties of files, such as size and modification
            dates.</p></item><item><p>Functions to perform basic operations on files as entities, such as copying,
            deletion, creating directories and establishment of temporary files.</p></item><item><p>Functions to read and write to and from files in various formats.</p></item><item><p>Functions to access and manipulate file paths.</p></item><item><p>Functions to examine properties of the underlying file system and current
            context, such as establishing the current working directory or the
            characters used for various separators within paths or files.</p></item></ulist><p>References to specific sections of other specifications are indicated by
        cross-document links in this document. Each such link consists of a pointer to a
        specific section followed a superscript specifying the linked document. The
        superscripts have the following meanings: FO <bibref ref="xpath-functions-40"/>,
        SER30 <bibref ref="ser30"/> and XS2 <bibref ref="xmlschema-2"/>
.</p><div2 id="error.management"><head>Error management</head><p>Error conditions are identified by a code (a <code nobreak="false">QName</code>.) When such an
          error condition is reached in the evaluation of an expression, a dynamic error
          is thrown, with the corresponding error code (as if the standard XPath function
          <code nobreak="false">error()</code> had been called.)</p><p>In this specification these codes use the <code nobreak="false">http://expath.org/ns/file</code>
          namespace and a “descriptive string” local part, e.g.
          <errorref spec="FILE40" code="not-found"/>, rather than the
          <code nobreak="false">http://www.w3.org/2005/xqt-errors</code> namespace and alpha-numeric local
          part, e.g. <code nobreak="false">err:FOCH0004</code>, used in <bibref ref="xpath-functions-40"/>. These error codes were chosen originally in the 1.0 version of 2013.</p></div2><div2 id="file-paths"><head>File Paths</head><p>File paths are specified as strings.</p><p><termdef id="current-working-dir" term="current working directory">
          The <term>current working directory</term> is an absolute
          <xtermref spec="FO40" ref="implementation-defined">implementation-defined</xtermref>
          directory against which all relative file paths are resolved.</termdef>
          For example, it can be the directory from which a query processor was started.</p><p><termdef id="base-dir" term="base directory">If a
          <xtermref spec="XP40" ref="dt-static-base-uri"/> exists,
          and if it points to a local file, the <term>base directory</term> points to the directory
          of this file. Otherwise, it is undefined.</termdef>
          <function>file:base-dir</function> can be used to retrieve the directory.
          A file path can be resolved against this directory by simply appending it (if it is relative),
          or by calling <function>file:resolve-path</function>:</p><eg xml:space="preserve">
file:base-dir() || $path,
file:resolve-path($path, file:base-dir())
        </eg><p>An implementation <rfc2119>must</rfc2119> accept absolute and relative UNIX/Linux
          and Windows paths as well as absolute file URIs. Some examples: </p><ulist><item><p><code nobreak="false">C:\Test Dir\my file.xml</code>: an absolute path on Windows platforms.</p></item><item><p><code nobreak="false">/Test Dir/my file.xml</code>: an absolute path on UNIX-based platforms.</p></item><item><p><code nobreak="false">C:\\\Test Dir//\\my file.xml</code>: an absolute path on Windows
              platforms that tolerates an arbitrary number of slashes and
              backslashes.</p></item><item><p><code nobreak="false">my file.xml</code>: a relative path, pointing to a file in the
              current working directory.</p></item><item><p><code nobreak="false">file:///C:/Test%20Dir/my%20file.xml</code>: an absolute file URI on
              Windows platforms.</p></item><item><p><code nobreak="false">file:///Test%20Dir/my%20file.xml</code>: an absolute path on
              UNIX-based platforms.</p></item></ulist><p>Before further processing, all paths are normalized to an
          <xtermref spec="FO40" ref="implementation-dependent">implementation-dependent</xtermref>
          representation (which usually is the representation of the underlying operating system),
          with operating-system-specific directory separators.</p><p>It is
          <xtermref spec="FO40" ref="implementation-dependent">implementation-dependent</xtermref>
          whether the error <errorref spec="FILE40" code="invalid-path"/> is raised if a
          path is invalid, i.e., if it contains characters that are not allowed in a specific
          file system.</p><p>Whenever a <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>
          function generates a path that is known to refer to a directory, it will be suffixed with
          the operating-system-specific directory separator.</p></div2><div2 id="exec"><head>Function Execution</head><p>Many functions are marked as
          <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>,
          which means they are not guaranteed to perform the same operations and produce
          identical results when being called repeatedly. A processor <rfc2119>must</rfc2119>
          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.</p></div2><div2 id="testing"><head>Test suite</head><p>A suite of test-cases for all the functions defined in this module, in
          <bibref ref="qt3"/> format, is defined at <bibref ref="tests.file"/>.</p></div2><div2 id="conformance"><head>Conformance</head><p>This specification follows the general remarks on and terminology for conformance
          given in <xspecref spec="FO40" ref="conformance"/></p><p>In this document, text labeled as an example or as a note is provided for
          explanatory purposes and is not normative.</p></div2><div2 id="namespace-prefixes"><head>Namespaces and prefixes</head><p>The functions defined in this document are contained exclusively in the namespace
          <code nobreak="false">http://expath.org/ns/file</code> and referenced using a
          <code nobreak="false">xs:QName</code> binding to that namespace.</p><p>This document uses the prefix <code nobreak="false">file</code> to refer to this namespace.
          User-written applications can choose a different prefix to refer to the
          namespace, so long as it is bound to the correct URI. In accordance with current
          practice, it is recommended that the prefix <code nobreak="false">file</code> be reserved for
          use with this namespace.</p></div2><div2 id="func-signatures"><head>Function signatures and descriptions</head><p>Each function (or group of functions having the same name) is defined in this
          specification using a standard proforma, full details of which can be found in
          <xspecref spec="FO40" ref="func-signatures"/>. In particular in this update
          (trailing) optional arguments for functions (introduced in XPath 4.0) are used
          where appropriate in the signatures, rather than multiple-arity signatures as
          previously. </p></div2></div1><div1 id="props"><head>File Properties</head><div2 id="func-file-name"><head>file:name</head><glist><gitem><label>Summary</label><def><p>Returns the name of a file or directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="name" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the name of a file or directory.
        An empty string is returned if the path points to the root directory.</p><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>:
        the path is neither checked for correctness nor looked up in the file system.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:name('')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">''</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:name('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">''</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:name('hello.txt')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'hello.txt'</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:name('dir/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'dir'</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:name('dir/file.txt')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'file.txt'</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:name('dir/..')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'..'</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:name('file:///tmp/001.bin')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'001.bin'</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-last-modified"><head>file:last-modified</head><glist><gitem><label>Summary</label><def><p>Returns the last modification time of a file or directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="last-modified" return-type="xs:dateTime" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns an <code nobreak="false">xs:dateTime</code> item representing the last modification of
        a file or directory.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:last-modified('.')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Returns the last modification time of the
          <termref def="current-working-dir"/>.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:last-modified(file:base-dir())</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Returns the last modification time of the
            <termref def="base-dir"/>.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-size"><head>file:size</head><changes><change issue="2016" PR="2077" date="2025-07-07"><code nobreak="false">$recursive</code> parameter added.</change></changes><glist><gitem><label>Summary</label><def><p>Returns the size of a file or directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="size" return-type="xs:integer" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/><arg name="recursive" type="xs:boolean?" default="false()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>
        If <code nobreak="false">$path</code> points to a file, returns the byte size of this file.
        Otherwise, if it points to a directory, returns either <code nobreak="false">0</code> or,
        if <code nobreak="false">$recursive</code> is <code nobreak="false">true</code>, the aggregated size of all files and
        subdirectories.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:size('.')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">0</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:size('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">0</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-exists"><head>file:exists</head><glist><gitem><label>Summary</label><def><p>Tests if a file or directory exists.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="exists" return-type="xs:boolean" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns <code nobreak="false">true</code> if <code nobreak="false">$path</code> points to an existing file or directory,
        <code nobreak="false">false</code> otherwise.</p><p>On a UNIX-based system, the root and the volume roots are considered directories.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:exists('.')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:exists('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-is-dir"><head>file:is-dir</head><glist><gitem><label>Summary</label><def><p>Tests if <code nobreak="false">$path</code> points to a directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="is-dir" return-type="xs:boolean" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns <code nobreak="false">true</code> if <code nobreak="false">$path</code> points to an existing directory
        as defined in <bibref ref="ieee1003.1-2024"/> (no regular file, no symbolic link,
        no other operating-system specific file type).
        Otherwise, it returns <code nobreak="false">false</code>.</p><p>On a UNIX-based system, the root and the volume roots are considered directories.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:is-dir('.')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:is-dir('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-is-file"><head>file:is-file</head><glist><gitem><label>Summary</label><def><p>Tests if <code nobreak="false">$path</code> points to a regular file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="is-file" return-type="xs:boolean" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns <code nobreak="false">true</code> if <code nobreak="false">$path</code> points to an existing regular file
        as defined in <bibref ref="ieee1003.1-2024"/> (no directory, no symbolic link,
        no other operating-system specific file type).
        Otherwise, it returns <code nobreak="false">false</code>.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:is-file('.')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">false()</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:is-file('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">false()</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-is-absolute"><head>file:is-absolute</head><changes><change issue="2016" PR="2077" date="2024-07-02">New in 4.0.</change></changes><glist><gitem><label>Summary</label><def><p>Tests if <code nobreak="false">$path</code> is absolute.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="is-absolute" return-type="xs:boolean" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns <code nobreak="false">true</code> if <code nobreak="false">$path</code> is absolute,
        <code nobreak="false">false</code> otherwise.</p><p>A path is absolute if it contains all the components necessary to identify a file location,
         without reference to a current drive or a current working directory.</p><note><p>All UNC file names are absolute.</p><p>The function may return a different result for the same path on a Windows and a
           UNIX-based system.</p></note></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:is-absolute('abc')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">false()</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:is-absolute('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Returns <code nobreak="false">true</code> on a UNIX-based system and
             <code nobreak="false">false</code> on a Windows system.</p></td></tr></tbody></table></def></gitem></glist></div2></div1><div1 id="entities"><head>Manipulation of Files as Entities</head><div2 id="func-file-copy"><head>file:copy</head><glist><gitem><label>Summary</label><def><p>Copies a file or a directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="copy" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="source" type="xs:string"/><arg name="target" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Copies a file or a directory given a source and a target path/URI.
        The following rules apply if <code nobreak="false">$source</code> points to a file:</p><olist><item><p>if <code nobreak="false">$target</code> does not exist, it will be created.</p></item><item><p>if <code nobreak="false">$target</code> is a file, it will be overwritten.</p></item><item><p>if <code nobreak="false">$target</code> 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.</p></item></olist><p>Otherwise, if <code nobreak="false">$source</code> points to a directory:</p><olist><item><p>if <code nobreak="false">$target</code> 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.</p></item><item><p>if <code nobreak="false">$target</code> 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.</p></item></olist><p>Other cases will result in one of the errors listed below.</p><p>The function returns the empty sequence if the operation is successful.
        No rollback to the original state will be possible if an error occurs during the operation.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="exists"/> is raised if the specified source path points to a
        directory and target path points to an existing file.</p><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified source path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified source path points to a
        file and the target path points to a directory in which a subdirectory exists
        with the name of the source file.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:copy('a.txt', 'b.txt')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Creates a copy of the file <code nobreak="false">a.txt</code> in the
            same directory, named <code nobreak="false">b.txt</code>.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:copy('a.txt', '../')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Creates a copy of the file <code nobreak="false">a.txt</code> with the same name
            in the parent directory.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:copy('dir/', 'dir2/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Creates a recursive copy of the directory <code nobreak="false">dir</code>
            in the <termref def="current-working-dir"/>, named <code nobreak="false">dir2</code>. If
            <code nobreak="false">dir2</code> already exists, the contents of <code nobreak="false">dir</code> will be copied
            into that directory.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-create-dir"><head>file:create-dir</head><glist><gitem><label>Summary</label><def><p>Creates a directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="create-dir" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="dir" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Creates a directory unless it already exists. The operation will also create
        non-existing parent directories.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="exists"/> is raised if the specified path, or any of
        its parent directories, points to an existing file.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:create-dir('examples')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Creates a directory named <code nobreak="false">examples</code> in the
            <termref def="current-working-dir"/>.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:create-dir('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Does nothing, as the root directory already exists.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-create-temp-dir"><head>file:create-temp-dir</head><changes><change issue="2016" PR="2077" date="2025-07-07">All parameters are optional now.</change></changes><glist><gitem><label>Summary</label><def><p>Creates a temporary directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="create-temp-dir" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="prefix" type="xs:string?" default="()"/><arg name="suffix" type="xs:string?" default="()"/><arg name="dir" type="xs:string?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Creates a temporary directory with an optional <code nobreak="false">$prefix</code> and
        <code nobreak="false">$suffix</code> in the filename, and returns the full path to the created directory.
        The path will point to a directory that did not exist before the function was called.</p><p>If <code nobreak="false">$dir</code> is omitted or an empty sequence, the directory will be created
        inside the operating-system specific default temporary-file directory.</p><note><p>The created directory will not be deleted automatically after query execution.</p></note></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the specified directory does not
        exist or points to a file.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">file:parent(file:create-temp-dir()) = file:temp-dir()</code></p></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">file:create-temp-dir(dir := file:base-dir())</eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Creates a temporary directory in the <termref def="base-dir"/> or,
            if it is undefined, in the default temporary-file directory.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
let $dir := file:create-temp-dir()
return try {
  file:write($dir || 'tmp.data', 1 to 10)
} finally {
  file:delete($dir, true())
}
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Creates a temporary directory, writes a file into that directory, and
            deletes it again.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-create-temp-file"><head>file:create-temp-file</head><changes><change issue="2016" PR="2077" date="2025-07-07">All parameters are optional now.</change></changes><glist><gitem><label>Summary</label><def><p>Creates a temporary file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="create-temp-file" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="prefix" type="xs:string?" default="()"/><arg name="suffix" type="xs:string?" default="()"/><arg name="dir" type="xs:string?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Creates an empty temporary file with an optional <code nobreak="false">$prefix</code> and
        <code nobreak="false">$suffix</code> in the filename, and returns the full path to the created file.
        The path will point to a file that did not exist before the function was called.</p><p>If <code nobreak="false">$dir</code> is omitted or an empty sequence, the file will be created inside
        the operating-system specific default temporary-file directory.</p><note><p>The created file will not be deleted automatically after query execution.</p></note></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the specified does not exist or
        points to a file.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:create-temp-file(suffix := '.txt')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Creates a file with the suffix <code nobreak="false">.txt</code> in the
            temporary-file directory.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-delete"><head>file:delete</head><changes><change issue="2547" PR="2548" date="2026-03-22">Non-existing paths are now ignored.</change></changes><glist><gitem><label>Summary</label><def><p>Deletes a file or a directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="delete" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/><arg name="recursive" type="xs:boolean?" default="false()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Deletes a file or a directory from the file system if it exists:</p><ulist><item><p>If <code nobreak="false">$path</code> points to a file, the file is deleted.</p></item><item><p>Otherwise, if <code nobreak="false">$path</code> points to a directory, then:</p><ulist><item><p>if it is non-empty and <code nobreak="false">$recursive</code> is <code nobreak="false">false</code>,
                 an error is raised.</p></item><item><p>Otherwise, the directory and all its contents are deleted recursively.</p></item></ulist></item><item><p>Otherwise, nothing happens.</p></item></ulist><p>The function returns the empty sequence if the operation is successful.</p><note><p>No rollback to the original state will be possible if an error occurs during the
           operation.</p></note></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        non-empty directory and if the deletion is not recursive.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:delete('list.txt')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Deletes the file <code nobreak="false">list.txt</code> from the
            <termref def="current-working-dir"/>.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:delete('examples', true())</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Deletes the file or directory <code nobreak="false">example</code> and (if available)
            all subdirectories.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-move"><head>file:move</head><glist><gitem><label>Summary</label><def><p>Moves a file or a directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="move" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="source" type="xs:string"/><arg name="target" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Moves a file or a directory given a source and a target path/URI.
        The following rules apply if <code nobreak="false">$source</code> points to a file:</p><olist><item><p>if <code nobreak="false">$target</code> does not exist, it will be created.</p></item><item><p>if <code nobreak="false">$target</code> is a file, it will be overwritten.</p></item><item><p>if <code nobreak="false">$target</code> 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.</p></item></olist><p>Otherwise, if <code nobreak="false">$source</code> points to a directory:</p><olist><item><p>if <code nobreak="false">$target</code> 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.</p></item><item><p>if <code nobreak="false">$target</code> 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 <errorref spec="FILE40" code="is-dir"/> is raised.</p></item></olist><p>Other cases will result in one of the errors listed below.</p><p>The function returns the empty sequence if the operation is successful.
        No rollback to the original state will be possible if an error occurs during the operation.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified source
          path does not exist.</p><p>
        <errorref spec="FILE40" code="exists"/> is raised if the specified source path points to a
        directory and target path points to an existing file.</p><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified source path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified target path points to a
        directory in which a subdirectory exists with the name of the source.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:move('a.txt', 'b.txt')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Renames <code nobreak="false">a.txt</code> to <code nobreak="false">b.txt</code>.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:move('a.txt', '../')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Moves the file <code nobreak="false">a.txt</code> to the parent directory.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:move('dir/', 'dir2/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Renames <code nobreak="false">dir</code> to <code nobreak="false">dir2</code>. If <code nobreak="false">dir2</code>
          already exists, moves the source directory into that directory.</p></td></tr></tbody></table></def></gitem></glist></div2></div1><div1 id="in-out"><head>Input/Output</head><div2 id="func-file-append"><head>file:append</head><changes><change issue="2016" PR="2077" date="2025-07-07"><code nobreak="false">$options</code> can now be a map, in alignment with <code nobreak="false">fn:serialize</code>.</change></changes><glist><gitem><label>Summary</label><def><p>Appends a serialized value to a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="append" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="value" type="item()*"/><arg name="options" type="(element(output:serialization-parameters) | map(*))?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Serializes a value and appends the resulting string to a file.
        If the file pointed to by <code nobreak="false">$file</code> does not exist,
        a new file will be created.</p><p>
        The <code nobreak="false">$options</code> argument controls the way how <code nobreak="false">$value</code> is serialized.
        The semantics are the same as for <xspecref spec="FO40" ref="func-serialize"/>.
        In contrast to <code nobreak="false">fn:serialize</code>, the encoding stage will not be skipped by
        this function.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified file does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:append('fragments.xml', &lt;fragment/&gt;)</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Appends a serialized element node to the file
            <code nobreak="false">fragments.xml</code>. The file is created if it does not already exist.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:append('snippets.json', { 'one': 1 }, { 'method': 'json' })</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Serializes a map as JSON and appends the resulting string to the file
            <code nobreak="false">snippets.json</code>.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-append-binary"><head>file:append-binary</head><glist><gitem><label>Summary</label><def><p>Appends binary data to a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="append-binary" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="value" type="xs:base64Binary"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Appends the binary data of an <code nobreak="false">xs:base64Binary</code> item to a file.
        If the file pointed to by <code nobreak="false">$file</code> does not exist, a new file will be
        created.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:append-binary('data.bin', xs:hexBinary('414243'))</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Appends the bytes <code nobreak="false">0x44</code>, <code nobreak="false">0x43</code> and <code nobreak="false">0x41</code>
            to the file <code nobreak="false">data.bin</code>.
            The file is created if it does not already exist.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-append-text"><head>file:append-text</head><glist><gitem><label>Summary</label><def><p>Appends a string to a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="append-text" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="value" type="xs:string"/><arg name="encoding" type="xs:string?" default="'UTF-8'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Appends a string to a file.
        If the file pointed to by <code nobreak="false">$file</code> does not exist, a new file will be
        created.</p><p>If no encoding is supplied, <code nobreak="false">UTF-8</code> is used as output encoding.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="unknown-encoding"/> is raised if the specified encoding
        is invalid or not supported by the implementation.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:append-text('todos.txt', 'clean up')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Appends a string to the file <code nobreak="false">todos.txt</code>.
            The file is created if it does not already exist.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-append-text-lines"><head>file:append-text-lines</head><glist><gitem><label>Summary</label><def><p>Appends a sequence of strings to a file, each followed by the operating-system specific
        newline character.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="append-text-lines" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="lines" type="xs:string*"/><arg name="encoding" type="xs:string?" default="'UTF-8'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Appends a sequence of strings to a file, each followed by the operating-system specific
        newline character.
        If the file pointed to by <code nobreak="false">$file</code> does not exist, a new file will be created.</p><p>If no encoding is supplied, <code nobreak="false">UTF-8</code> is used as output encoding.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="unknown-encoding"/> is raised if the specified encoding
        is invalid or not supported by the implementation.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:append-text-lines('numbers.txt', (1 to 5) ! string())</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Appends the string representation of the integers 1 to 5 to the file
            <code nobreak="false">numbers.txt</code>. The file is created if it does not already exist.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-read-binary"><head>file:read-binary</head><changes><change issue="2016" PR="2077" date="2025-07-07"><code nobreak="false">$length</code> can now be supplied without supplying <code nobreak="false">$offset</code>.</change></changes><glist><gitem><label>Summary</label><def><p>Returns the binary content of a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="read-binary" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="offset" type="xs:integer?" default="0"/><arg name="length" type="xs:integer?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the content of a file as an <code nobreak="false">xs:base64Binary</code> item.</p><p>Chunks of a file can be read by supplying <code nobreak="false">$offset</code> and <code nobreak="false">$length</code>.
        If no value is supplied for <code nobreak="false">$length</code>, all remaining bytes will be read.
        If only <code nobreak="false">$length</code> is specified, the offset is <code nobreak="false">0</code>.
      </p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="out-of-range"/> is raised if the specified offset or length
        value is negative, or if the value would exceed the file bounds.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:read-binary('data.bin') eq xs:hexBinary('41')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Returns <code nobreak="false">true</code> if the file <code nobreak="false">data.bin</code>
            contains the single byte <code nobreak="false">0x41</code>.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:read-binary('data.bin', file:size($path) - 1, 1)</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Returns the last byte of the file <code nobreak="false">data.bin</code>.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-read-text"><head>file:read-text</head><changes><change issue="2016" PR="2077" date="2024-07-02"><code nobreak="false">$options</code> parameter added.</change><change issue="2016" PR="2077" date="2024-07-02">The normalization of newlines has been made explicit.</change><change issue="2460" PR="2510" date="2026-03-11">A fallback option is now provided to address characters that cannot be
         decoded or are not permitted.</change></changes><glist><gitem><label>Summary</label><def><p>Returns the content of a file as a string.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="read-text" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="options" type="(xs:string | map(*))?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the content of a file in its string representation.
        Newlines are normalized: Any <char>U+000D</char> character, optionally followed by
        a <char>U+000A</char> character, is converted to a single <char>U+000A</char> character.</p><p>The <code nobreak="false">$options</code> argument, for backwards compatibility reasons, may be supplied
         either as a map, or as a string. Supplying a value <code nobreak="false">$S</code> that is not a map
         is equivalent to supplying the map <code nobreak="false">{ "encoding": $S }</code>.
         After that substitution, the <xtermref spec="FO40" ref="option-parameter-conventions"/> apply.</p><p>The entries that may appear in the <code nobreak="false">$options</code> map are as follows:</p><example role="record"><record returnEmptyOk="no" returnSeq="no" prefix="fn" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no" isOp="no"><arg name="encoding" type="xs:string?" occur="opt"/><arg name="fallback" type="xs:boolean" occur="opt"/></record></example><table class="fos-options"><thead><tr><th rowspan="1" colspan="1">Key</th><th rowspan="1" colspan="1">Meaning</th></tr></thead><tbody><tr><td rowspan="1" colspan="1"><p><code nobreak="false">encoding?</code></p></td><td class="fos-thin" rowspan="1" colspan="1">Defines the encoding of the resource, as described below.
               <ulist><item><p><term>Type: </term><code nobreak="false">xs:string?</code></p></item><item><p><term>Default: </term><code nobreak="false">"UTF-8"</code></p></item></ulist></td></tr><tr><td rowspan="1" colspan="1"><p><code nobreak="false">fallback?</code></p></td><td class="fos-thin" rowspan="1" colspan="1">
                  If <code nobreak="false">$fallback</code> is true, any character that 
                  cannot be decoded or that is not a <xtermref spec="FO40" ref="dt-permitted-character"/>
                  is replaced by the Unicode replacement character
                  (<char>U+FFFD</char>).
               <ulist><item><p><term>Type: </term><code nobreak="false">xs:boolean</code></p></item><item><p><term>Default: </term><code nobreak="false">false</code></p></item></ulist></td></tr></tbody></table></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="unknown-encoding"/> is raised if the specified encoding
        is invalid or not supported by the implementation.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">contains(file:read-text('todos.txt'), 'push forward')</code></p></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Returns <code nobreak="false">true</code> if the file <code nobreak="false">todos.txt</code>
            contains the specified string.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
'test.bin'
=&gt; file:write-binary(xs:hexBinary('00'))
=&gt; file:read-text(fallback := true())
=&gt; string-to-codepoints()
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">65533</eg><p><emph>(Returns the codepoint value of the Unicode replacement character.)</emph></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-read-text-lines"><head>file:read-text-lines</head><changes><change issue="2016" PR="2077" date="2024-07-02"><code nobreak="false">$fallback</code> parameter added.</change><change issue="2460" PR="2510" date="2026-03-11">The <code nobreak="false">$fallback</code> parameter has been integrated into the options map.</change></changes><glist><gitem><label>Summary</label><def><p>Returns the contents of a file as a sequence of strings, separated at newline
        boundaries.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="read-text-lines" return-type="xs:string*" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="options" type="(xs:string | map(*))?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the contents of a file as a sequence of strings, separated at newline
        boundaries.</p><p>Any of the character sequences <char>U+000A</char>, <char>U+000D</char>, or
         <char>U+000D</char> followed by <char>U+000A</char> is interpreted as newline.</p><p>The <code nobreak="false">$options</code> argument, for backwards compatibility reasons, may be supplied
         either as a map, or as a string. Supplying a value <code nobreak="false">$S</code> that is not a map
         is equivalent to supplying the map <code nobreak="false">{ "encoding": $S }</code>.
         After that substitution, the <xtermref spec="FO40" ref="option-parameter-conventions"/> apply.</p><p>The entries that may appear in the <code nobreak="false">$options</code> map are as follows:</p><example role="record"><record returnEmptyOk="no" returnSeq="no" prefix="fn" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no" isOp="no"><arg name="encoding" type="xs:string?" occur="opt"/><arg name="fallback" type="xs:boolean" occur="opt"/></record></example><table class="fos-options"><thead><tr><th rowspan="1" colspan="1">Key</th><th rowspan="1" colspan="1">Meaning</th></tr></thead><tbody><tr><td rowspan="1" colspan="1"><p><code nobreak="false">encoding?</code></p></td><td class="fos-thin" rowspan="1" colspan="1">Defines the encoding of the resource, as described below.
               <ulist><item><p><term>Type: </term><code nobreak="false">xs:string?</code></p></item><item><p><term>Default: </term><code nobreak="false">"UTF-8"</code></p></item></ulist></td></tr><tr><td rowspan="1" colspan="1"><p><code nobreak="false">fallback?</code></p></td><td class="fos-thin" rowspan="1" colspan="1">
                  If <code nobreak="false">$fallback</code> is true, any character that 
                  cannot be decoded or that is not a <xtermref spec="FO40" ref="dt-permitted-character"/>
                  is replaced by the Unicode replacement character
                  (<char>U+FFFD</char>).
               <ulist><item><p><term>Type: </term><code nobreak="false">xs:boolean</code></p></item><item><p><term>Default: </term><code nobreak="false">false</code></p></item></ulist></td></tr></tbody></table></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="unknown-encoding"/> is raised if the specified encoding
        is invalid or not supported by the implementation.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
let $file := 'numbers.txt'
let $data := (1 to 5) ! string()
return (
  file:write-text-lines($file, $data),
  file:read-text-lines($file) =&gt; deep-equal($data)
)
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">true()</eg><p><emph>(Can be used to prove that the written and read strings are equal.)</emph></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-write"><head>file:write</head><changes><change issue="2016" PR="2077" date="2025-07-07"><code nobreak="false">$options</code> can now be a map, in alignment with <code nobreak="false">fn:serialize</code>.</change></changes><glist><gitem><label>Summary</label><def><p>Writes a serialized value to a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="write" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="value" type="item()*"/><arg name="options" type="(element(output:serialization-parameters) | map(*))?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Serializes a value and appends the resulting string to a file.
        If the file pointed to by <code nobreak="false">$file</code> already exists, it will be overwritten;
        otherwise, it will be created.</p><p>
        The <code nobreak="false">$options</code> argument controls the way how <code nobreak="false">$value</code> is serialized.
        The semantics are the same as for <xspecref spec="FO40" ref="func-serialize"/>.
        In contrast to <code nobreak="false">fn:serialize</code>, the encoding stage will not be skipped by
        this function.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">file:write('numbers.txt', 1 to 10)</code></p></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Writes 10 numbers to the file <code nobreak="false">numbers.txt</code>.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
file:write(
  'result.xml',
  &lt;result&gt;&lt;done/&gt;&lt;/result&gt;,
  { 'encoding': 'us-ascii', 'indent': true() }
)
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Serializes an indented element node as <code nobreak="false">US-ASCII</code> and
            writes it to the file <code nobreak="false">result.xml</code>.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
file:write(
  'numbers.json',
  map:build(1 to 10, string#1),
  { 'method': 'json' }
)
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Writes a map, serialized as JSON, to a specified file.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-write-binary"><head>file:write-binary</head><glist><gitem><label>Summary</label><def><p>Writes binary data to a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="write-binary" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="value" type="xs:base64Binary"/><arg name="offset" type="xs:integer?" default="0"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Writes the binary data of an <code nobreak="false">xs:base64Binary</code> item to a file.
        If the file pointed to by <code nobreak="false">$file</code> already exists, it will be overwritten;
        otherwise, it will be created.</p><p>If <code nobreak="false">$offset</code> is specified and not an empty sequence, data will be written 
        starting at this file position, and existing bytes will be overwritten.
        The operation may resize the existing file.</p><p>The function returns the empty sequence if the operation is successful.</p><note><p>Supplying an offset makes sense only if the file already exists.</p></note></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="out-of-range"/> is raised if the specified offset is negative,
        or if it exceeds the current file size.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:write-binary('data.bin', xs:hexBinary('414243'))</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Writes the bytes <code nobreak="false">0x44</code>, <code nobreak="false">0x43</code> and <code nobreak="false">0x41</code>
            to the file <code nobreak="false">data.bin</code>.</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:write-binary('data.bin', xs:hexBinary('44', 2))</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>If this expression is called after the previous one, overwrites the last
            byte with <code nobreak="false">0x44</code>.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-write-text"><head>file:write-text</head><glist><gitem><label>Summary</label><def><p>Writes a string to a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="write-text" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="value" type="xs:string"/><arg name="encoding" type="xs:string?" default="'UTF-8'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Writes a string to a file.
        If the file pointed to by <code nobreak="false">$file</code> already exists, it will be overwritten.</p><p>If no encoding is supplied, <code nobreak="false">UTF-8</code> is used as output encoding.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="unknown-encoding"/> is raised if the specified encoding
        is invalid or not supported by the implementation.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:write-text('todos.txt', 'get organized')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Writes a string to the file <code nobreak="false">todos.txt</code>.
            The file is created if it does not already exist.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-write-text-lines"><head>file:write-text-lines</head><glist><gitem><label>Summary</label><def><p>Writes strings to a file.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="write-text-lines" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="file" type="xs:string"/><arg name="values" type="xs:string*"/><arg name="encoding" type="xs:string?" default="'UTF-8'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Writes strings to a file, each followed by the operating-system specific newline character.
        If the file pointed to by <code nobreak="false">$file</code> already exists, it will be overwritten;
        otherwise, it will be created.</p><p>If no encoding is supplied, <code nobreak="false">UTF-8</code> is used as output encoding.</p><p>The function returns the empty sequence if the operation is successful.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the parent directory of
        the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="is-dir"/> is raised if the specified path points to a
        directory.</p><p>
        <errorref spec="FILE40" code="unknown-encoding"/> is raised if the specified encoding
        is invalid or not supported by the implementation.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:write-text-lines('numbers.txt', (1 to 5) ! string())</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Writes the string representation of the integers 1 to 5 to the file
            <code nobreak="false">numbers.txt</code>.</p></td></tr></tbody></table></def></gitem></glist></div2></div1><div1 id="paths"><head>Paths</head><div2 id="func-file-parent"><head>file:parent</head><glist><gitem><label>Summary</label><def><p>Returns the path to the parent directory of a given path.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="parent" return-type="xs:string?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Transforms the given path into an absolute path, as specified by
        <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#fn.resolve-path" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">file:resolve-path</loc>, and returns the parent
        directory.</p><p>An empty sequence is returned if the path points to a root directory.</p><note><p>The inverse function is <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#func-file-children" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">file:children</loc>.</p></note></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:parent('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">()</code></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:current-dir() = file:parent('abc')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-children"><head>file:children</head><glist><gitem><label>Summary</label><def><p>Returns the paths of all files and directories that are located in the given
        directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="children" return-type="xs:string*" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>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 references to the specified directory and its parent directory
        (<code nobreak="false">.</code> and <code nobreak="false">..</code>) are not returned.</p><note><p>The inverse function is <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#func-file-parent" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">file:parent</loc>; a related function
        is <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#func-file-list" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">file:list</loc>.</p></note></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the specified path
        does not point to an existing directory.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">count(file:children('.'))</eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Counts the files and directories in the <termref def="current-working-dir"/>.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1">
            <eg xml:space="preserve">file:children('path/to/media/')
      [matches(., '\.(avi|mpg|mp4)$', 'i')]</eg>
          </td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Returns the paths to large videos found in a specific directory.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-descendants"><head>file:descendants</head><changes><change issue="2016" PR="2077" date="2024-07-02">New in 4.0.</change></changes><glist><gitem><label>Summary</label><def><p>Returns the paths of all files and directories that are located in the given
        directory and its subdirectories.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="descendants" return-type="xs:string*" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the paths of all files and directories that are located in the given
        directory and its subdirectories. The order of the items in the resulting sequence
        is not defined.
        The references to the specified directory and its parent directory
        (<code nobreak="false">.</code> and <code nobreak="false">..</code>) are not returned.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the specified path
        does not point to an existing directory.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">count(file:descendants('.')[file:is-directory(.)])</code></p></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Counts the subdirectories in the <termref def="current-working-dir"/>.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
for $file in file:descendants('.')
where file:last-modified($file) &gt; current-dateTime() - xs:dayTimeDuration('PT1H')
return $file
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Returns the paths to all files that have been modified in the last hour.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-list"><head>file:list</head><changes><change issue="2016" PR="2077" date="2025-07-07"><code nobreak="false">$pattern</code> can now be supplied without supplying <code nobreak="false">$recursive</code>.</change></changes><glist><gitem><label>Summary</label><def><p>Lists all files and directories in a given directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="list" return-type="xs:string*" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="dir" type="xs:string"/><arg name="recursive" type="xs:boolean?" default="false()"/><arg name="pattern" type="xs:string?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Lists all files and directories in a given directory.
        In contrast to is <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#func-file-children" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">file:children</loc>,
        the returned paths are relative to the supplied directory <code nobreak="false">$dir</code>.
        The order of the items in the resulting sequence is not defined.
        The references to the specified directory and its parent directory
        (<code nobreak="false">.</code> and <code nobreak="false">..</code>) are not returned.</p><p>If <code nobreak="false">$recursive</code> is <code nobreak="false">true</code>, all directories and files will be returned
        that are found while recursively traversing the given directory.
        Otherwise, only the contents of the specified directory will be returned.</p><p>The <code nobreak="false">$pattern</code> argument is used to define a name pattern in the glob syntax.
        If supplied, only the paths of the files and directories whose names are matching the pattern
        will be returned.</p><p>An implementation must support at least the following glob syntax characters:</p><ulist><item><p><code nobreak="false">*</code> for matching any number of unknown characters and</p></item><item><p><code nobreak="false">?</code> for matching one unknown character.</p></item></ulist><note><p>A related function is <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="#func-file-children" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">file:children</loc>.</p></note></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="no-dir"/> is raised if the specified path does not point
        to an existing directory.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if any other error occurs.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">file:list('.')</code></p></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Returns the names of all files in the
            <termref def="current-working-dir"/>.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">file:list('.', pattern := '*.zip')</code></p></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Returns the names of archive files in the
            <termref def="current-working-dir"/>.</p></td></tr><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
let $root := '/path/to/files/'
for $file in file:list($root, true(), '*.txt')
let $path := file:resolve-path($file, $root)
where file:size($path) &gt; 1000000
return file:read-text($path)
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Returns the contents of large text files found in a specific
            directory and its subdirectories.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-list-roots"><head>file:list-roots</head><changes><change issue="2016" PR="2077" date="2024-07-02">New in 4.0.</change></changes><glist><gitem><label>Summary</label><def><p>Lists all root directories of the file system.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="list-roots" return-type="xs:string*" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"/></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Lists all root directories of the file system:</p><ulist><item><p>On a Windows system, root directories are usually the letters of the
            currently available drives, followed by a colon and a backslash
            (for example, <code nobreak="false">C:\</code> and <code nobreak="false">D:\</code>).</p></item><item><p>On a UNIX-based system, it is usually a single slash, denoting the root directory.</p></item></ulist><note><p>Dynamically available UNC file names are not returned by this function.</p></note></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:list-roots('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>A single slash as result indicates a UNIX-based system.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-resolve-path"><head>file:resolve-path</head><changes><change issue="2016" PR="2077" date="2024-07-02"><code nobreak="false">$base</code> parameter added.</change></changes><glist><gitem><label>Summary</label><def><p>Transforms a relative path into an absolute operating system path.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="resolve-path" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/><arg name="base" type="xs:string?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Transforms a relative path into an absolute operating system path.
        The following rules apply in order:</p><olist><item><p>If <code nobreak="false">$path</code> is an absolute path, it is not changed.</p></item><item><p>Otherwise, if <code nobreak="false">$base</code> is the empty sequence, <code nobreak="false">$path</code> is
            resolved against the <termref def="current-working-dir"/>.</p></item><item><p>Otherwise, it is resolved against the supplied base path or, if the base path does
            not point to a directory, to its parent directory.
            An error is raised if the base is a relative path.</p></item></olist><p>If the resulting path points to a directory, it will be suffixed with the
        operating-system specific directory separator.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="is-relative"/> is raised if the specified base directory
          is relative.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:resolve-path('INF', 'C:/Windows/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'C:/Windows/INF/'</code></p><p><emph>(The result refers to a Windows system.)</emph></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:resolve-path('data.bin', 'C:/Temp')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'C:/data.bin'</code></p><p><emph>(The result refers to a Windows system.)</emph></p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:resolve-path('hilda/notes.txt', '/home/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">'/home/hilda/notes.txt'</code></p><p><emph>(The result refers to a UNIX-based system.)</emph></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-path-to-native"><head>file:path-to-native</head><glist><gitem><label>Summary</label><def><p>Transforms a path to a canonical representation.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="path-to-native" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Transforms a URI, an absolute path, or relative path to a canonical, operating-system specific
        path representation. A canonical path is both absolute and unique and thus contains
        no redirections such as references to parent directories or symbolic links.</p><p>If the resulting path points to a directory, it will be suffixed with the
       operating-system specific directory separator.</p></def></gitem><gitem><label>Error Conditions</label><def><p>
        <errorref spec="FILE40" code="not-found"/> is raised if the specified path does not exist.</p><p>
        <errorref spec="FILE40" code="io-error"/> is raised if an error occurs while trying
        to generate the native path.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:path-to-native('/')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Returns <code nobreak="false">/</code> on a UNIX-based system.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-path-to-uri"><head>file:path-to-uri</head><glist><gitem><label>Summary</label><def><p>Transforms a file system path into a URI.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="path-to-uri" return-type="xs:anyURI" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="path" type="xs:string"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Transforms a file system path into a URI with the <code nobreak="false">file</code> scheme. If the
        path is relative, it is first resolved against the <termref def="current-working-dir"/>.</p><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>:
        the path is neither checked for correctness nor looked up in the file system.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:path-to-uri('/temp')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Returns <code nobreak="false">file:///temp</code> on a UNIX-based system</p></td></tr><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">contains(file:path-to-uri('a b'), '%20')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p><p><emph>(Space characters will be encoded to <code nobreak="false">%20</code>.)</emph></p></td></tr></tbody></table></def></gitem></glist></div2></div1><div1 id="sys-props"><head>System Properties</head><div2 id="func-file-dir-separator"><head>file:dir-separator</head><glist><gitem><label>Summary</label><def><p>Returns the directory separator.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="dir-separator" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"/></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the value of the operating-system specific directory separator, which usually
        is <code nobreak="false">/</code> on UNIX-based systems and <code nobreak="false">\</code> on Windows systems.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:dir-separator() = ('/', '\')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-line-separator"><head>file:line-separator</head><glist><gitem><label>Summary</label><def><p>Returns the line separator.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="line-separator" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"/></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the value of the operating-system specific line separator, which usually is
        <code nobreak="false">&amp;#10;</code> on UNIX-based systems, <code nobreak="false">&amp;#13;&amp;#10;</code> on
        Windows systems and <code nobreak="false">&amp;#13;</code> on old Mac systems.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">matches(file:line-separator(), '^(\n|\r\n|\r)$')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-path-separator"><head>file:path-separator</head><glist><gitem><label>Summary</label><def><p>Returns the path separator.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="path-separator" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"/></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the value of the operating-system specific path separator, which usually is
        <code nobreak="false">:</code> on UNIX-based systems and <code nobreak="false">;</code> on Windows systems.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:dir-separator() = (':', ';')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p><code nobreak="false">true()</code></p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-temp-dir"><head>file:temp-dir</head><glist><gitem><label>Summary</label><def><p>Returns the path to the temporary-file directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="temp-dir" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"/></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the path to the default temporary-file directory of an operating system.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><thead><tr><th rowspan="1" colspan="1">Expression</th><th rowspan="1" colspan="1">Result</th></tr></thead><tbody><tr><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve"><code nobreak="false">file:write-text(file:temp-dir() || 'todos.txt', 'get on going')</code></eg></td><td valign="top" rowspan="1" colspan="1"><p>Write a text string to a file in the temporary-file directory.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-base-dir"><head>file:base-dir</head><glist><gitem><label>Summary</label><def><p>Returns the base directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="base-dir" return-type="xs:string?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"/></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the <termref def="base-dir"/>.
        If defined, the function returns the same result as the expression
        <code nobreak="false">file:parent(static-base-uri())</code>. Otherwise, it returns an empty sequence</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
let $dir := file:base-dir() otherwise file:create-temp-dir()
return file:write-text($dir || 'todos.txt', 'get up')
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><p>Writes <code nobreak="false">todos.txt</code> to the <termref def="base-dir"/> or,
if it is not defined, to a temporary directory.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-file-current-dir"><head>file:current-dir</head><glist><gitem><label>Summary</label><def><p>Returns the current working directory.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="file" name="current-dir" return-type="xs:string" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"/></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-nondeterministic">nondeterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-dependent">context-dependent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the <termref def="current-working-dir"/>.
      All relative file paths are resolved against this directory.       
      </p></def></gitem><gitem><label>Formal Equivalent</label><def><p>The effect of the function is equivalent to the result of the following XPath expression.</p><eg xml:space="preserve">file:resolve-path('.')</eg></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">
file:path-to-native('todos.txt') =
file:path-to-native(file:current-dir() || 'todos.txt')
          </eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">true()</eg><p><emph>(Both paths refer to the same file.)</emph></p></td></tr></tbody></table></def></gitem></glist></div2></div1></body><back><div1 id="biblio"><head>References</head><div2 id="normative-biblio"><head>Normative references</head><blist><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="expath-file" key="EXPath File 1.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            <loc href="http://expath.org/spec/file" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">File Module 1.0</loc>. Christian
            Grün, Matthias Brantner and Gabriel Petrovay, editors. EXPath Module 20
            February 2015.</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="ieee754" key="IEEE 754-1985" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">IEEE Standard for Binary Floating-Point Arithmetic.
            See <loc href="http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html</loc>.
          </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="ieee754-2019" key="IEEE 754-2019" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">IEEE.
            <emph>IEEE Standard for Floating-Point Arithmetic.</emph>
          </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath20" key="XML Path Language (XPath) 2.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath-functions-40" key="XQuery and XPath Functions and Operators 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath-40" key="XPath 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            
          </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xslt-40" key="XSL Transformations (XSLT) Version 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            <emph>CITATION: T.B.D.</emph>
          </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath-datamodel-40" key="XQuery and XPath Data Model (XDM) 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="ser30" key="XSLT and XQuery Serialization 3.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            <loc href="http://www.w3.org/TR/xslt-xquery-serialization-30/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XSLT and XQuery
            Serialization 3.0</loc>. Henry Zongaro, et al. W3C Recommendation 08 April 2014.</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="fo30" key="XQuery and XPath Functions and Operators 3.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            <loc href="http://www.w3.org/TR/xpath-functions-30/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XPath and XQuery
            Functions and Operators 3.0</loc>. Michael Kay. W3C Recommendation 08 April 2014.</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xquery-40" key="XQuery 4.1: An XML Query Language" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            <emph>CITATION: T.B.D.</emph>
          </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xmlschema-2" key="XML Schema Part 2: Datatypes Second Edition" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"> XML
            Schema Part 2: Datatypes Second Edition, Oct. 28 2004. Available at:
            <loc href="http://www.w3.org/TR/xmlschema-2/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/xmlschema-2/</loc>
          </bibl></blist></div2><div2 id="non-normative-biblio"><head>Non-normative references</head><blist><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="tests.file" key="Test-suite" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">The test suite for this module, using QT3
            format, is in the EXPath repository
            <loc href="http://github.com/expath/expath-cg/tree/master/tests/qt3/file" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://github.com/expath/expath-cg</loc> in the directory tests/qt3/file/</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="qt3" key="QT3" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            <loc href="http://dev.w3.org/2011/QT3-test-suite/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Query Test Suite</loc>.
            W3C 21 June 2013. </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="expath" key="EXPath" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">EXPath: Collaboratively Defining Open Standards
            for Portable XPath Extensions.
            <loc href="http://expath.org/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://expath.org/</loc>.</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="ieee1003.1-2024" key="IEEE 1003.1-2024" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
            <titleref href="https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">Open Group Base Specifications Issue 8</titleref>. IEEE, 2024.
          </bibl></blist></div2></div1><div1 id="error-summary"><head>Error summary</head><p>The error text provided with these errors is non-normative.</p><error-list><error spec="FILE40" code="not-found"><p>The specified path does not exist.</p></error><error spec="FILE40" code="invalid-path"><p>The specified path is invalid.</p></error><error spec="FILE40" code="exists"><p>The specified path already exists.</p></error><error spec="FILE40" code="no-dir"><p>The specified path does not point to a directory.</p></error><error spec="FILE40" code="is-dir"><p>The specified path points to a directory.</p></error><error spec="FILE40" code="is-relative"><p>The specified path is relative.</p></error><error spec="FILE40" code="unknown-encoding"><p>The specified encoding is not supported.</p></error><error spec="FILE40" code="out-of-range"><p>The specified offset or length is negative, or the chosen values would exceed
          the file bounds.</p></error><error spec="FILE40" code="io-error"><p>A generic file system error occurred.</p></error></error-list></div1><inform-div1 id="glossary"><head>Glossary</head><?glossary?></inform-div1><inform-div1 id="other-functions"><head>Other Functions</head><p>This Appendix describes some sources of functions or operators that fall outside the
      scope of the function library defined in this specification. It includes both
      function specifications and function implementations. Inclusion of a function in
      this appendix does not constitute any kind of recommendation or endorsement; neither
      is omission from this appendix to be construed negatively. This Appendix does not
      attempt to give any information about licensing arrangements for these function
      specifications or implementations.</p><div2 id="functions-in-other-w3c-specs"><head>XPath Functions and Operators Defined in Other W3C Recommendations</head><p>A number of W3C Recommendations make use of XPath, and in some cases such
        Recommmendations define additional functions to be made available when XPath is
        used in a specific host language.</p><div3 id="functions-defined-in-XPathFO"><head>Functions and Operators Defined in XPath and XQuery Functions and Operators</head></div3></div2></inform-div1><inform-div1 id="impl-def"><head>Checklist of implementation-defined features</head><?imp-def-features?></inform-div1><inform-div1 id="changelog" diff="chg" at="2024-11-22"><head>Changes since version 1.0</head><div2 id="changes-summary"><head>Summary of Changes</head><?change-log?></div2><div2 id="changes-to-function-signatures"><head>Changes to Function Signatures</head><olist><item><p>The function signatures of all the specified signatures now use the
            “optional argument” syntax of XPath 4.0 where appropriate, rather than
            giving several signatures of differing arity. Other than that, no
            intended change to the semantics of the functions are assumed.</p></item></olist></div2><div2 id="editorial-changes"><head>Editorial Changes</head><p>These changes are not highlighted in the change-marked version of the specification.</p></div2></inform-div1><inform-div1 id="back-compatibility" diff="chg" at="A"><head>Compatibility with Previous Versions</head><p>This section summarizes the extent to which this specification is compatible with
      previous versions.</p><p>Version 4.0 of this function library is fully backwards compatible with version 1.0,
      except as noted below:</p><olist><item diff="add" at="2024-11-25"><p>The use of optional arguments in the function signatures means that minor
          alterations to possible function calls, which would be invalid in 1.0, are
          now supported. For example:</p><eg xml:space="preserve">file:list($dir, false(), ())</eg><p>would be invalid in 1.0, as the third argument <code nobreak="false">$pattern</code> is
          defined to be of type <code nobreak="false">xs:string</code>. It is valid for 4.0, as the
          empty sequence denotes default behavior of listing all files. </p><p>The functions <code nobreak="false">file:append</code>, <code nobreak="false">file:append-text</code>,
          <code nobreak="false">file:append-text-lines</code>,
          <code nobreak="false">file:create-temp-dir</code>, <code nobreak="false">file:create-temp-file</code>,
          <code nobreak="false">file:delete</code>, <code nobreak="false">file:read-binary</code>,
          <code nobreak="false">file:read-text</code>, <code nobreak="false">file:read-text-lines</code>,
          <code nobreak="false">file:write</code>, <code nobreak="false">file:write-binary</code>,
          <code nobreak="false">file:write-text</code> and <code nobreak="false">file:write-text-lines</code> all have
          similar incompatibilities.</p></item></olist></inform-div1></back></spec><!--XSLT Processor: Saxonica-->