[ You are here: XTF -> Programming -> File Utilities ]

Checking File Existence, Length, and Modification Time

XSLT allows a stylesheet to easily pull in the contents of an XML file, by using the document() function. However, it does not provide a convenient way to check whether the file exists before opening it, nor to find out its length or the time it was last modified. While these could be accomplished using Saxon's default ways of using any Java class, it would be quite clumsy. Instead, XTF provides a set of XSLT extension functions to fill this functionality gap.

Here's an example showing how we can check for a file's existence:
<xsl:stylesheet xmlns:FileUtils="java:org.cdlib.xtf.xslt.FileUtils"><xsl:variable name="filename"
    select="concat('../../', $sourceDir, $docId, '.mets.xml')"/>
  <xsl:choose>
    <xsl:when test="FileUtils:exists($filename)" >
      <xsl:message>File exists.</xsl:message>
    </xsl:when>
    <xsl:otherwise>
      <xsl:message>File doesn't exist.</xsl:message>
    </xsl:otherwise>
  </xsl:choose>
In this way you can put together a filename, check whether the file exists, and perform different actions depending on that check. XTF also provides functions to find out the length of a file, and to determine the last time it was modified. For more information, please refer to the XTF Tag Reference section on FileUtils.