Test whether file exists

Here should go questions about transforming XML with XSLT and FOP.
Posts: 29

Test whether file exists

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: 77

Re: Test whether file exists

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: 2478

Re: Test whether file exists

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

Re: Test whether file exists

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

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests