Test whether file exists

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

Test whether file exists

Post by mboudreau » Fri Mar 24, 2017 11:30 pm

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

Code: Select all

<xsl:when test="fs:exists(fs:new('AJS405045.xml'))" xmlns:fs="java.io.File">
<xsl:message><xsl:text>Found it!</xsl:text></xsl:message>
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

Where do I start to troubleshoot this?

Posts: 78
Joined: Thu Oct 23, 2008 6:29 am

Re: Test whether file exists

Post by Jamil » Mon Mar 27, 2017 10:28 am

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?

Posts: 2531
Joined: Tue May 17, 2005 4:01 pm

Re: Test whether file exists

Post by adrian » Mon Mar 27, 2017 10:59 am

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'.

Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger

Posts: 29
Joined: Sat Jan 07, 2017 1:23 am

Re: Test whether file exists

Post by mboudreau » Thu Mar 30, 2017 10:39 pm

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">

Code: Select all

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

Post Reply