How do I check for the existence of an external file with XSL?
No, this cannot be done using XSLT 2.0/XPath 2.0.
The XSLT 2.0 function unparsed-text-available() is only suitable for locating text files and even if a text file with the specifies URI exists this function may return false(), because it also must read the contents of the file and check that it only contains allowed characters.
From the spec:
"The unparsed-text-available function determines whether a call on the unparsed-text function with identical arguments would return a string.
If the first argument is an empty sequence, the function returns false. If the second argument is an empty sequence, the function behaves as if the second argument were omitted.
In other cases, the function returns true if a call on unparsed-text with the same arguments would succeed, and false if a call on unparsed-text with the same arguments would fail with a non-recoverable dynamic error.
Note:
This requires that the unparsed-text-available function should actually attempt to read the resource identified by the URI, and check that it is correctly encoded and contains no characters that are invalid in XML "
End of quotation.
EDIT: Use unparsed-text-available function. It is part of xslt 2.0, but not XQuery or standalone XPath.
I've left my previous answer here so you can follow the trail of uncertainty...
I don't believe there is a way of doing this in XSLT using the standard functions. You can do it using extension functions, as described here, for java.
There is the unparsed-text-available function, but I'm unsure if this is a standard function. There's an example of it's usage at Zvon. The unparsed-text-available is mentioned here as being part of xslt 2.0, and is supported in Saxon.
In case someone else needs it and is using fop, I would like to share this answer since it worked for me like a charm.
I've found a solution:
<xsl:when test="fs:exists(fs:new('myfile.html'))" xmlns:fs="java.io.File">
<!-- do something here... -->
</xsl:when>
and it works independently of XSLT 1.0 or 2.0
For anyone else who might stumble upon this old question, by piecing together information from various sources on the internet, I came up with this XSLT2 function that uses a Java extension for checking whether a file exists:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:java="http://www.java.com/"
exclude-result-prefixes="java xs">
<xsl:function name="java:file-exists" xmlns:file="java.io.File" as="xs:boolean">
<xsl:param name="file" as="xs:string"/>
<xsl:param name="base-uri" as="xs:string"/>
<xsl:variable name="absolute-uri" select="resolve-uri($file, $base-uri)" as="xs:anyURI"/>
<xsl:sequence select="file:exists(file:new($absolute-uri))"/>
</xsl:function>
</xsl:stylesheet>
You can then use the function like this:
<xsl:if test="java:file-exists($filename, base-uri())">
<!-- ... -->
</xsl:if>
This works at least with Saxon 9.1.0.5J — haven't tested it with any other XSLT processors.
Note: If the file for whose existence you're checking is an XML file and you're using XSLT2, you can also use the built-in doc-available()
function:
<xsl:if test="doc-available('hello_world.xml')">
<!-- ... -->
</xsl:if>