Test whether file exists

Here should go questions about transforming XML with XSLT and FOP.
mboudreau
Posts: 68
Joined: Sat Jan 07, 2017 1:23 am

Test whether file exists

Post by mboudreau »

I'm creating a transformation during which I need to know whether a file exists. I found this solution online:

Code: Select all


    <xsl:choose>
<xsl:when test="fs:exists(fs:new('AJS405045.xml'))" xmlns:fs="java.io.File">
<xsl:message><xsl:text>Found it!</xsl:text></xsl:message>
</xsl:when>
<xsl:otherwise>
<xsl:message><xsl:text>Missing!</xsl:text></xsl:message>
</xsl:otherwise>
</xsl:choose>
but after adding this to my stylesheet, the test always fails, but I'm not seeing any error message. (The named file is in the same directory as the stylesheet itself.)

My transformation is running with Saxon-PE 9.6.0.7.

Where do I start to troubleshoot this?
Jamil
Posts: 97
Joined: Thu Oct 23, 2008 6:29 am

Re: Test whether file exists

Post by Jamil »

Your use case is not making sense to me. Based on your description, XSLT does not seem appropriate for what you wish to do. Why not test for file existence in Java instead?
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Test whether file exists

Post by adrian »

Hi,
the test always fails
So, the transformation is successful but it always goes the xsl:otherwise route. Is that correct?
If you're running this on Linux (also applies to some OS X file systems), the file names are case sensitive, so check the exact file name (uppercase/lowercase letters).
I would also recommend testing with an absolute file path. So, instead of 'AJS405045.xml' use '/path/to/my/file/AJS405045.xml'.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
mboudreau
Posts: 68
Joined: Sat Jan 07, 2017 1:23 am

Re: Test whether file exists

Post by mboudreau »

Sorry for the late reply--I neglected to check "Notify me when a reply is posted".

Thanks, Adrian: specifying the absolute file path solved the problem.

FWIW, I'm running this in Oxygen 18.1 on OS X 10.11.6 (El Capitan), so I'm accustomed to assuming filenames are case-sensitive.

Using the absolute path works with both of the following tests:

Code: Select all

<xsl:when test="file:exists('mrb.txt')" xmlns:file="http://expath.org/ns/file">
and

Code: Select all

<xsl:when test="fs:exists(fs:new('mrb.txt'))" xmlns:fs="java.io.File">
Post Reply