[xsl] Test if an (image) file exists

Subject: [xsl] Test if an (image) file exists
From: "Derek Revill" <derek@xxxxxxxxxxxxxxxxxx>
Date: Wed, 8 Jun 2005 12:52:40 +0100
Hello,

I'd like to test if image files exist in a target directory on my file
system before adding links to them in html output (I need to check on
$filename.jpg or $filename.gif and other variations.) Should I use a Java
extension function in my XSLT or are there now better ways to do this with
in-built XSLT functions, using unparsed-text() for example? 

I'm have an XSLT 2.0 sheet and I'm running Saxon 8.4 (with Java VM 1.4). 

I'm no Java programmer so an exact code snippet of a template calling Java
would be great. I turned up this older example in the archive:

http://www.biglist.com/lists/xsl-list/archives/200006/msg01332.html

<xsl:template name="file_util_check_directory" xmlns:file="java.io.File">
  <xsl:param name="filename" />
  <xsl:variable name="directory"
                select="file:new(file:getParent(file:new($filename)))" />
  <xsl:if test="not(file:exists($directory))">
    <xsl:choose>
      <xsl:when test="file:mkdirs($directory)">
        <xsl:message>
          <xsl:text>Creating directory `</xsl:text>
          <xsl:value-of select="file:getPath($directory)" />
          <xsl:text>'</xsl:text>
        </xsl:message>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>
          <xsl:text>WARNING : unable to create directory `</xsl:text>
          <xsl:value-of select="file:getPath($directory)" />
          <xsl:text>'.</xsl:text>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template> <!-- name="file_util_check_directory" -->

But if I pass this template a filename (xs:string) I get the following
error:

"There is more than one method matching the function call file:new, and
there is insufficient type information to determine which one should be
used." 

Any help appreciated.

Derek

--
http://www.revill.demon.co.uk

Current Thread