This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: how to test if a file exists ?


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]