RE: [xsl] Test if an (image) file exists

Subject: RE: [xsl] Test if an (image) file exists
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 8 Jun 2005 21:36:32 +0100
> 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? 

You need to use extension functions. Assuming that the reference to the
image is in an attribute called @src, and that it takes the form of a
relative URI (relative to the document that contains the reference), you
will need something like this:

<xsl:variable name="absoluteURI" select="resolve-uri(@src, base-uri(.))"
as="xs:anyURI"/>
<xsl:if test="file:exists(file:new($absoluteURI))"
xmlns:file="java@xxxxxxxxxxxx">...

> 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." 
> 
Java provides File.new(string) and File.new(URI), so Saxon needs to know at
compile time whether the argument is a string or a URI. In this case the
function was called as file:new($param) where $param was declared as

<xsl:param name="param"/>

with no type information. You can probably fix the problem by changing the
declaration to

<xsl:param name="param" as="xs:string"/>

or by writing the function call as file:new($param as xs:string)

(or use xs:anyURI if that's what it is)

Michael Kay
http://www.saxonica.com/

Current Thread