This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: how to test if a file exists ?
- To: xsl-list at mulberrytech dot com
- Subject: Re: how to test if a file exists ?
- From: Warren Hedley <w dot hedley at auckland dot ac dot nz>
- Date: Thu, 29 Jun 2000 09:58:34 -0400
- CC: Jean-Claude Tarby <Jean-Claude dot Tarby at cmis dot csiro dot au>
- Organization: Department of Engineering Science, The University of Auckland
- References: <3.0.1.32.20000629205832.0140eaf0@snowy.nsw.cmis.csiro.au>
- Reply-To: xsl-list at mulberrytech dot com
Jean-Claude Tarby wrote:
>
> I want to include links to graphic files in a html document which is
> produced by a stylesheet, but I want to put the link only if the graphic
> file exists on the hard disk. How to do that ?
You have to use an extension function - the following syntax will
work in both Saxon and XT. You can actually use it to reference
any external Java class, but you're pretty much guaranteed that
java.io.File will always be available!
I use this template to check if a directory exists, and if not,
to create it. You can easily adapt it to your problem.
<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" -->
--
Warren Hedley
Department of Engineering Science
Auckland University
New Zealand
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list